2013-12-20 32 views
4

我参考了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> 

...

我的广播接收器下面的代码:

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(); 
} 
+0

挑战你知道,如果你的'BroadcastReceiver'被称为呢?添加一些日志记录来查看它是否被调用。 –

+0

我在onReceive中添加了日志。但是当我点击卸载按钮时,我没有得到任何日志。 :( – dickfala

+0

如何在没有任何启动程序的情况下运行你的应用程序? –

回答

0

一般来说,当你卸载应用程序,所有资源从设备中删除。因此在卸载时您不能触发任何操作

+0

如果检测到用户单击卸载按钮(不卸载),应用程序可以在logcat中记录程序包名称,我们可以使用logcat info parse packagename我认为引用的意思是[this](http://stackoverflow.com/questions/18692571/how-it-works-warning-that-app-is-going-to-be-uninstalled)。但我可以't实施@@。 – dickfala

+0

它就像我们需要自定义卸载意图操作 –

+0

你可以发布如何自定义卸载意图操作吗?我真的需要学习如何实现。非常感谢你。 – dickfala

1

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

参见“关于停止应用程序启动控制”一节中http://developer.android.com/about/versions/android-3.1.html


编辑:在安卓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代码在4.0设备上测试过这个。如果你安装你的应用程序,那么你去Settings->Apps并选择另一个应用程序(不是你的)已被“强制停止”,你的onReceive()被称为Intent.ACTION_QUERY_PACKAGE_RESTART

我意识到这可能没有太大的帮助,但它至少可以解释你所看到的行为。

对不起了这么久来解决这个问题,并感谢:-)

+0

我曾尝试添加活动。但是我仍然无法接收广播内容。@@ dickfala

+0

实际启动应用程序(即:启动活动)安装应用程序后? –

+0

是的,我已经启动了我的活动。我附上我的源代码网站。 https://www.dropbox.com/s/0bgbcfn0a0gfoe3/UninstallDetected.zip :( – dickfala