应用程序卸载时无法获取接收器

时间:2013-12-20 08:52:41

标签: android android-intent broadcastreceiver uninstall

我提到this信息。

我编写了相同的程序,但是当我点击卸载按钮时,我无法获得任何日志信息。

我在下面附上我的代码。有谁知道这段代码中有什么问题?

我想在使用点击卸载按钮时执行某些操作。也许喜欢打开浏览器等等。

有人可以帮我一把吗?

这个问题困扰了我很长时间。

我的AndroidManifest:

...        

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
 <receiver android:name=".UninstallIntentReceiver" >
        <intent-filter>
           <action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
           <action  android:name = "android.intent.action.PACKAGE_REMOVED"  />
           <action  android:name = "android.intent.action.PACKAGE_ADDED"  />
           <data android:scheme="package"></data>
        </intent-filter>
    </receiver>
</application>

...

我的BroadcastReceiver代码如下:

public class UninstallIntentReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    String [] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
    if(packageNames!=null){
        for(String packageName: packageNames){
            if(packageName!=null && packageName.equals("yu.idv.uninstalldetected")){
                Log.i("info", "00000000"); 
               new ListenActivities(context).start();
               Log.i("info", "11111111");

            }
        }
    }
  }

}

class ListenActivities extends Thread{
boolean exit = false;
ActivityManager am = null;
Context context = null;

public ListenActivities(Context con){
    context = con;
    am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}

public void run(){

    Looper.prepare();

    while(!exit){

         List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(MAX_PRIORITY);

         String activityName = taskInfo.get(0).topActivity.getClassName();


         Log.i("info", "======CURRENT Activity =======::"
                 + activityName);

         if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
             exit = true;
             Log.i("info", "2222222222");                
            Toast.makeText(context, "Done with preuninstallation tasks... Exiting Now",            Toast.LENGTH_SHORT).show();
        } else if(activityName.equals("com.android.settings.ManageApplications")) {
            exit=true;
             Log.i("info", "33333333333");
        }
    }
    Looper.loop();
}

2 个答案:

答案 0 :(得分:1)

未调用BroadcastReceiver的原因是您的应用程序仍处于“停止状态”。您的应用程序没有任何Activity组件。这意味着在安装应用程序后,用户无法启动它。用户从未启动的应用程序保持“停止状态”。处于“已停止状态”的应用程序将不会收到广播意图

请参阅http://developer.android.com/about/versions/android-3.1.html

中“启动已停止的应用程序的控件”部分

编辑:在Android 4.x中添加有关Intent.ACTION_QUERY_PACKAGE_RESTART的更多详细信息

从Android 2.3开始,这种行为似乎发生了变化(我不确定Android 3.x)。在Android 4.0及更高版本中,InstalledAppDetails(“设置”应用程序的一部分)中的代码如下所示:

 private void checkForceStop() {
     if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
         // User can't force stop device admin.
         updateForceStopButton(false);
     } else if ((mAppEntry.info.flags&ApplicationInfo.FLAG_STOPPED) == 0) {
         // If the app isn't explicitly stopped, then always show the
         // force stop button.
         updateForceStopButton(true);
     } else {
         Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART,
                 Uri.fromParts("package", mAppEntry.info.packageName, null));
         intent.putExtra(Intent.EXTRA_PACKAGES, new String[] { mAppEntry.info.packageName });
         intent.putExtra(Intent.EXTRA_UID, mAppEntry.info.uid);
         getActivity().sendOrderedBroadcast(intent, null, mCheckKillProcessesReceiver, null,
                 Activity.RESULT_CANCELED, null, null);
     }
 }

因此,如果显示的应用程序是设备管理员,则“强制停止”按钮将被禁用。如果显示的应用程序未停止,则将启用“强制停止”按钮。 否则,Intent.ACTION_QUERY_PACKAGE_RESTART将被广播。

这意味着Intent只会针对已停止的非设备管理应用广播。

我使用您的代码在4.0设备上对此进行了测试。如果您安装了应用程序,那么您转到Settings->Apps并选择已经“强制停止”的其他应用程序(不是您的应用程序)onReceive()将被{{1}调用}}

我意识到这可能没什么帮助,但它至少解释了你所看到的行为。

很抱歉,花了这么长时间才解决这个问题,并感谢您的挑战: - )

答案 1 :(得分:0)

通常,在卸载应用程序时,会从设备中删除所有资源。所以你不能在卸载时触发任何动作

相关问题