【问题标题】:Capture PACKAGE_REMOVED event with BroadcastReceiver使用 BroadcastReceiver 捕获 PACKAGE_REMOVED 事件
【发布时间】:2012-09-27 12:47:42
【问题描述】:

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

【问题讨论】:

  • 你所拥有的东西看起来肯定没问题,尽管intent 永远不会是null,所以这个特定的测试并不是特别有用。
  • 好吧测试 - 我这样做是因为我想确保动作测试有时不会出现异常(事实上我保护自己免受我对 Android 内部消息传递系统的无知)。这就是说,取出整个代码,除了登录到文件之外,它仍然不执行CustomBroadcastReceiver ...

标签: android broadcastreceiver package


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多