简体   繁体   English

动态注册后,Android Broadcast Receiver不会唤醒应用程序

[英]Android Broadcast Receiver not waking up application when registered dynamically

I have a situation where I only want to register a BroadcastReceiver for Time changed on certain platforms. 我遇到的情况是,我只想注册在某些平台上更改时间的BroadcastReceiver For performance reasons, I would like to register the receiver dynamically via the context.registerReceiver method only on the platforms needed. 出于性能原因,我只想在所需的平台上通过context.registerReceiver方法动态注册接收器。 I can detect the platform on app first run and decide if I would like to listen to this event. 我可以在应用程序首次运行时检测该平台,并决定是否要收听此事件。

While testing this approach I have found that when registering via context.registerReceiver the application will not be woken up if the process is not running. 在测试这种方法时,我发现通过context.registerReceiver注册时,如果进程未运行,则不会唤醒应用程序。

Is this expected and is there a setting to get around it? 这是预期的,并且有解决的方法吗?

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);

When your broadcast registered dynamically you can receive messages only when your application is running. 动态注册广播后,只有在应用程序运行时才能接收消息。 If you want it to wake up you need to declare your broadcast in manifest 如果要唤醒它,则需要在清单中声明广播

A dynamically registered receiver has a short lifetime so it wont wake up your device always. 动态注册的接收器的寿命很短,因此不会始终唤醒您的设备。 You have to declare it in your Manifest if you want it to behave they way you need! 如果要使其表现为所需的样子,则必须在清单中声明它!

PS: Had exactly the same problem recently so i am pretty sure for this! PS:最近有完全一样的问题,所以我对此很确定!

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

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