使用BroadcastReceiver捕获PACKAGE_REMOVED事件

时间:2012-10-06 17:06:28

标签: android broadcastreceiver package

我想在删除包时捕获事件。 我使用以下内容: BroadcastReceiver子类:

public class CustomBroadcastReceiver extends BroadcastReceiver {

    /**
     * This method captures the event when a package has been removed
     */
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Helper.writeInLogFile("Hello from CustomBroadcastReceiver");
        if (intent != null) {
            String action = intent.getAction();         
            if (action.equals(intent.ACTION_PACKAGE_REMOVED))   {
                //Log the event capture in the log file ...
                Helper.writeInLogFile("The package has been removed");
            }
        }
    }
}

带有显示部分:

<receiver android:name="CustomBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" >
                </action>
            </intent-filter>
</receiver>

...但是没有触发CustomBroadcastReceiver。我究竟做错了什么? 感谢

1 个答案:

答案 0 :(得分:0)

答案就在这里:https://groups.google.com/forum/?fromgroups=#!topic/android-developers/aX5-fMbdPR8在'hackbod'2 / 28/08下。原则上,对于任何事件处理程序,需要在系统中注册CustomBroadcastReceiver实例。我的代码遗漏了这一点。当然,注册(上面链接中的代码)需要在应用程序的主要活动类中完成。作为一个注释,如果我们在主要活动中定义了intent过滤器,我们就不能在清单中提及它们(它不会中断,但它是多余的)。