0

我想在删除包时捕获事件。我使用以下内容: 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 没有被解雇。我究竟做错了什么?谢谢

4

1 回答 1

0

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

于 2012-10-08T10:41:45.633 回答