Android广播接收器在动态注册时不会唤醒应用程序

标签 android android-intent

我有一种情况,我只想注册一个 BroadcastReceiver 用于在某些平台上更改时间。出于性能原因,我想仅在需要的平台上通过 context.registerReceiver 方法动态注册接收器。我可以在应用程序首次运行时检测平台并决定是否要收听此事件。

在测试这种方法时,我发现当通过 context.registerReceiver 注册时,如果进程未运行,应用程序将不会被唤醒。

这是预期的吗?是否有设置来解决它?

IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_CHANGED); 
this.getApplicationContext().registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.e("MYTAG", "Received Broadcast from dyn one");


        }
    }, filter);

最佳答案

当您的广播动态注册时,您只能在您的应用程序运行时接收消息。如果你想让它醒来你需要在 list 中声明你的广播

关于Android广播接收器在动态注册时不会唤醒应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18237517/

相关文章:

android - 棉花糖上的 ACTION_ASSIST Intent (Android 6.0)

java - 格式化字符串时%df代表什么?

android - 在 kotlin 中检查互联网连接 android

android - 在默认音乐播放器上播放歌曲 - android

android-intent - 确定意图返回的android中uri的Mime类型?

android - Intent 在 android 中是异步的。为什么不能是同步调用呢?

android - 如何从 Intent 过滤器中获取 Activity 类

android - 如何在 xamarin forms/Android 中正确使用 SVG 图像?

android - FusedLocationProvider - 精确定位和 wifi

java - 从大到小排序列表