[英]Can't get receiver when the app uninstall
我引用這些信息。
我編寫了相同的程序,但是當我單擊卸載按鈕時,我無法獲得任何日志信息。
我在下面附上我的代碼。 有誰知道這段代碼中有什么問題?
我想在使用點擊卸載按鈕時做一些事情。 也許喜歡打開瀏覽器等
有人可以幫我一把嗎?
這個問題困擾了我很長一段時間。
我的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();
}
不調用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
(Settings應用程序的一部分)中的代碼如下所示:
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
並選擇另一個已經“強制停止”的應用程序(不是您的應用程序) ,使用Intent.ACTION_QUERY_PACKAGE_RESTART
調用onReceive()
。
我意識到這可能沒什么幫助,但它至少解釋了你所看到的行為。
對不起,解決這個問題花了這么長時間,並感謝您的挑戰:-)
通常,在卸載應用程序時,將從設備中刪除所有資源。 因此,在卸載時無法觸發任何操作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.