簡體   English   中英

如何獲取Android應用程序嘗試從設備中刪除/卸載的操作

[英]How to get action that android application is trying removed/uninstalled from device

當用戶想要從Android設備卸載應用程序時,我想要該應用程序的用戶卸載按鈕單擊事件。

我正在從設備中刪除應用程序的事件,但我想在刪除應用程序之前顯示彈出窗口。 我正在嘗試實現與“App Lock”應用程序相同的功能。

這是我通過廣播接收器獲取應用程序刪除事件的代碼。 但是關於卸載按鈕單擊或彈出單擊之前,我完全是空白。 請指導我正確的方向。

提前致謝。

public class MainActivity extends Activity {
    CustomBroadcastReceiver mApplicationsReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mApplicationsReceiver=new CustomBroadcastReceiver();

        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); 
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 
        filter.addAction(Intent.ACTION_PACKAGE_ADDED); 
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED); 
        filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
        filter.addAction(Intent.ACTION_PACKAGE_VERIFIED);
        filter.addAction(Intent.ACTION_PACKAGE_INSTALL);
        filter.addAction(Intent.ACTION_PACKAGE_FIRST_LAUNCH);

        filter.addAction(Intent.ACTION_DELETE); 
        filter.addAction(Intent.ACTION_DEFAULT); 
        filter.addDataScheme("package"); 
        registerReceiver(mApplicationsReceiver, filter); 
    }





}


public class CustomBroadcastReceiver extends BroadcastReceiver {

    /**
     * This method captures the event when a package has been removed
     */
    @Override
    public void onReceive(Context context, Intent intent)
    {
        System.out.println("Hello from CustomBroadcastReceiver");
        if (intent != null) {
            String action = intent.getAction();   
            System.out.println("L1123 : "+action);
            if (action.equals(intent.ACTION_PACKAGE_REMOVED))   {
                //Log the event capture in the log file ...
                System.out.println("The package has been removed");
            }
        }
    }
}



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bits.uninstallappdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />


    <uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.DELETE_PACKAGES" />


    <uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
    <uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
    <uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
    <uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

       <!--  <receiver android:name=".CustomBroadcastReceiver" >
            <intent-filter>


                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />


                <category android:name="android.intent.category.DEFAULT" />

                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_CHANGED" />
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
            </intent-filter>
        </receiver> -->
    </application>

</manifest>

請嘗試通過ActivityManager獲取任務中的頂級ActivityManager ,並檢查它是否是卸載活動。

核心代碼:

ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;
String packageName = topActivity.getPackageName();
String className = topActivity.getClassName();
Log.v(TAG, "packageName" + packageName);
Log.v(TAG, "className" + className);

if ("com.android.packageinstaller".equals(packageName)
    && "com.android.packageinstaller.UninstallerActivity".equals(className)) {
//Do anything you want here
}

您正在使用的以下權限僅授予系統應用程序。 確保您擁有root設備以允許此類權限。

<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM