在android中卸载应用程序时调用广播接收器

Vik*_*tel 6 android android-package-managers android-broadcast

我想在应用程序上清理我的应用程序创建的垃圾UnInstalling.

使用ManiFest文件: -

在清单文件中添加:

 <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>
Run Code Online (Sandbox Code Playgroud)

创建接收器以捕获 BroadCast Event

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

在Java代码: -

 BroadCastReceiver br = new CleanReceiver();
 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
 intentFilter.addDataScheme("package");
 registerReceiver(br, intentFilter);
Run Code Online (Sandbox Code Playgroud)

但是在卸载应用程序时,接收器从未被调用过.

Java和Manifest都不会在卸载应用程序时调用Receiver.如何在卸载应用程序时捕获广播事件?

caf*_*991 5

您可以广播任何其他要卸载的软件包的广播,但不能广播自己的软件包。

原因

发生这种情况的原因是,当您在自己的应用程序中注册卸载接收器时,并且在卸载应用程序时,已注册的BroadcastReceiver在应用程序卸载之前已被卸载,因此该BroadcastReceiver不会接收其自身的卸载事件。

试想一下这样一种情况,即广播已注册(例如,短信接收者)并且该应用即将被卸载。现在广播中出现短信会检测到它,但广播的应用程序(创建它的)已被卸载。这可能导致不一致可能就是多数民众赞成在它发生的原因。