Call broadcast receiver at time of uninstalling application in android [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-06 00:36:50

问题


I want to clean up the junk created by my application at time on UnInstalling the Application.

Using ManiFest File:-

Added in Manifest File:

 <receiver android:name="com.netdoers.com.ui.CleanReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REMOVED" >
            </action>
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>

Created Receiver to catch the BroadCast Event

public class CleanReceiver extends BroadcastReceiver
{
  public void onReceive(Context context, Intent intent) {
    CustomToast.showToastMessage(context, "Uninstalling Application");
    Log.e("Uninstall", "CleanReceiver Called");
  }
} 

In Java Code:-

 BroadCastReceiver br = new CleanReceiver();
 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
 intentFilter.addDataScheme("package");
 registerReceiver(br, intentFilter);

But at time of Uninstalling application the receiver is never been called.

Both Java and Manifest never call Receiver at event of Uninstall application. How to catch the broadcast event at time of Uninstalling application?


回答1:


You can get the broadcast for any other package getting uninstalled but never for your own package.

Reason

That happens because when you register uninstall receiver in your own app and when the app is uninstalled, the registered BroadcastReceiver has been uninstalled before the app gets uninstalled,so its own uninstallation event won't be received by that BroadcastReceiver.

Just think of a scenario that say a broadcast is registered(say a sms receiver) and the app is about to get uninstalled.Now sms comes in broadcast detects it but the broadcast's application(which created it) got uninstalled.The may lead to inconsistency in the system.So may be thats the reason it happens.



来源:https://stackoverflow.com/questions/21989572/call-broadcast-receiver-at-time-of-uninstalling-application-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!