简体   繁体   English

Android:BroadcastReceiver:意图为UNINSTALL_PACKAGE

[英]Android: BroadcastReceiver: UNINSTALL_PACKAGE intent

I need to detect when my app is being uninstalled. 我需要检测何时卸载我的应用程序。 For that, I've seen logcat sends out UNINSTALL_PACKAGE intent, and I simply added it to existing Broadcast Receiver I have. 为此,我已经看到logcat发送出UNINSTALL_PACKAGE意图,并且我只是将其添加到了我现有的广播接收器中。 But it just doesn't catch it, while other intent I'm listening to, TIME_TICK, works perfectly. 但是它只是无法捕捉到它,而我正在听的其他意图TIME_TICK可以完美地工作。 Why? 为什么?

Code currently used: 当前使用的代码:

    private IntentFilter intentFilter;

static {
    intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_TIME_TICK);
    intentFilter.addAction(Intent.ACTION_UNINSTALL_PACKAGE);
}

private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (action.equals(Intent.ACTION_UNINSTALL_PACKAGE) {
            Log.e("App", "Uninstalling");
        } else if (action.equals(Intent.ACTION_TIME_TICK){
        // this works
        }
    }
};

I need to detect when my app is being uninstalled 我需要检测何时卸载我的应用

There is no supported way to do this, for obvious security reasons. 出于明显的安全原因,没有支持的方法来执行此操作。

But it just doesn't catch it 但这没抓住

ACTION_UNINSTALL_PACKAGE is an activity action . ACTION_UNINSTALL_PACKAGE是一项活动动作 Nothing on an Android device should be sending it as a broadcast. Android设备上的任何内容都不应将其作为广播发送。 Hence, you cannot listen to it via a BroadcastReceiver . 因此,您无法通过BroadcastReceiver收听它。

I 've seen logcat sends out UNINSTALL_PACKAGE intent, and I simply added it to existing Broadcast Reciever I have. 我已经看到logcat发出了UNINSTALL_PACKAGE意图,我只是将其添加到了我现有的广播接收器中。 But it just doesn't catch it, while other intent I'm listening to, TIME_TICK, works perfectly. 但是它只是无法捕捉到它,而我正在听的其他意图TIME_TICK可以完美地工作。 Why? 为什么?

Whenever your application is being uninstalled, Android OS kills all the components associated with that application as well free memory & resources. 每当您卸载应用程序时,Android OS都会杀死与该应用程序关联的所有组件以及可用的内存和资源。 Uninstall broadcast receiver intent was to listen uninstall event of others app not for your app. Uninstall广播接收器的意图是监听其他应用程序的卸载事件,而不是您的应用程序。

If you want to capture uninstall event of your app then there is no concept google provides, however you can do by observing data change in your file system where you need to keep monitoring changes in file system. 如果您想捕获应用程序的uninstall事件,那么google没有提供任何概念,但是您可以通过观察文件系统中的数据更改来实现,而您需要继续监视文件系统中的更改。

This solution is not going to work for all phones & different versions of Android. 此解决方案不适用于所有手机和不同版本的Android。

当您使用ACTION_DELETEACTION_UNINSTALL_PACKAGE卸载时,是否可以仅使用IntentFilter监听android.intent.action.PACKAGE_REMOVED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM