4

当用户想从 android 设备卸载应用程序时,我希望用户卸载该应用程序的按钮单击事件。

我正在从设备中删除应用程序事件,但我想在删除应用程序之前显示弹出窗口。我正在尝试实现与在“应用程序锁定”应用程序中相同的效果。

这是我通过广播接收器获取应用程序删除事件的代码。但是我对卸载按钮单击或弹出单击之前完全空白。请指导我正确的方向。

提前致谢。

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>
4

2 回答 2

4

请尝试通过 获取任务中的top Activity ActivityManager,并检查是否为卸载Activity。

核心代码:

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
}
于 2013-09-05T11:44:51.393 回答
1

您正在使用的以下权限仅授予系统应用程序。确保您有根设备以允许此类权限。

<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" />
于 2013-09-05T11:56:22.700 回答