简体   繁体   English

Android:如果应用程序进程被杀死,动态注册的广播接收器将被激活

[英]Android: will the dynamically registered broadcast receiver be activated if the app process has been killed

As I know, statically registered (via manifest) broadcast receiver will be activated when the broadcast is fired, it doesn't require the app process is running. 据我所知,静态注册(通过清单)广播接收器将在广播被激活时被激活,它不需要应用程序进程正在运行。 But what about the dynamically registered one? 但是动态注册的呢?

AFAIK, You can make your Broadcast Receiver can run in background even your application is closed or destroyed or killed. AFAIK,即使您的应用程序已关闭,销毁或被杀,您也可以使您的广播接收器可以在后台运行。

If you want to do above one , you should not be registering it via registerReceiver(). 如果你想做一个以上,你不应该通过registerReceiver()注册它。 Register it in the manifest via a element instead. 通过元素在清单中注册它。 Then, it is available whether or not your application is running. 然后,无论您的应用程序是否正在运行,它都可用。

One more option if you want the broadcast receiver to killed or stopped whenever your application is closed or destroyed or killed. 如果您希望广播接收器在应用程序关闭,销毁或终止时被杀死或停止,还有一个选项。

you should call/invoke registerReceiver() method in your onCreate and You should call/invoke unregisterReceiver() in onResume() or in onpause() methods as per your need you can use this. 你应该在你的onCreate中调用/调用registerReceiver()方法你应该根据你的需要在onResume()或onpause()方法中调用/调用unregisterReceiver()你可以使用它。

One of the differences between BroadcastReceiver that declared in AndroidManifest.xml and the one that registered with Context.registerReceiver() is that first one is instantiated by the Android system, when the second one - by the application code. AndroidManifest.xml中声明的BroadcastReceiver与在Context.registerReceiver()中注册的一个之间的区别之一是,第一个是由Android系统实例化的,第二个是由应用程序代码实例化的。 When application process is terminated, all it's data (including all objects and jvm itself) is destroyed. 当应用程序进程终止时,它的所有数据(包括所有对象和jvm本身)都将被销毁。 So the only way to handle broadcast Intent for your receiver is to start new application process, instantiate new YourBroadcastReceiver and call its onReceive() method (and this is what it does for receivers, declared in manifest). 因此,为接收器处理广播Intent的唯一方法是启动新的应用程序进程,实例化新的YourBroadcastReceiver并调用其onReceive()方法(这就是它对接收器所做的,在清单中声明)。 But in case of receiver that was dynamically registered with registerReceiver() the system gets just some receiver instance, but not a mechanism for its creation. 但是在使用registerReceiver()动态注册的接收器的情况下,系统只获得一些接收器实例,但不是它的创建机制。 Furthermore, if for example your receiver class is non-static inner class then there is no reasonable way to instantiate it by the external (system) code because the system could not know, in which state the external object (and application) should be, to receiver worked properly. 此外,例如,如果您的接收器类是非静态内部类,那么没有合理的方法通过外部(系统)代码实例化它,因为系统无法知道外部对象(和应用程序)应该处于哪种状态,接收器工作正常。 Also constructor could have arguments. 构造函数也可以有参数。

So if the process is terminated, your dynamically registered BroadcastReceiver will never called until you register new one in a new process. 因此,如果进程终止,您的动态注册的BroadcastReceiver将永远不会调用,直到您在新进程中注册新的。

As the android documentation says: 正如android 文档所说:

When you are defining broadcast receiver in your app manifest(Manifest-declared receivers), 当您在应用清单(Manifest声明的接收器)中定义广播接收器时,

The system package manager registers the receiver when the app is installed. 系统软件包管理器在安装应用程序时注册接收器。 The receiver then becomes a separate entry point into your app which means that the system can start the app and deliver the broadcast if the app is not currently running. 然后,接收器成为应用程序的单独入口点,这意味着如果应用程序当前未运行,系统可以启动应用程序并发送广播。

But when you are dynamically registering the broadcast receiver(Context-registered receivers), 但是当您动态注册广播接收器(上下文注册的接收器)时,

Context-registered receivers receive broadcasts as long as their registering context is valid. 只要注册上下文有效,上下文注册的接收器就会接收广播。 For an example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. 例如,如果您在“活动”上下文中注册,则只要活动未被销毁,您就会收到广播。 If you register with the Application context, you receive broadcasts as long as the app is running. 如果您在应用程序上下文中注册,则只要应用程序正在运行,您就会收到广播。

So your dynamically registered broadcast receiver will not be activated when your application is killed. 因此,当您的应用程序被杀死时,您的动态注册广播接收器将不会被激活

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

相关问题 当应用程序被杀时,Android广播接收器无法正常工作 - Android Broadcast Receiver not working when the app is killed 已在Android中注册BroadCast接收器 - Registered BroadCast Receiver in Android Android:如何对onResume / onPause方法中的广播接收器进行注册/注销的功能测试 - Android: how to functional test that a broadcast receiver has been registered/unregistered in onResume/onPause methods 我的Android APP进程已被终止,但仍在RunningTask中 - My Android APP's process has been killed, but still in RunningTask 动态注册后,Android Broadcast Receiver不会唤醒应用程序 - Android Broadcast Receiver not waking up application when registered dynamically 如果应用程序在 android oreo 及更早版本中被杀死,则显式广播接收器不会启动 - Explicit Broadcast receiver dont launch if app is killed in android oreo and earlier 当应用程序在 android 版本 9 中被杀死时收听呼叫广播接收器 - listen to call broadcast receiver when app gets killed in android version 9 Android广播接收器未注册错误 - Android broadcast receiver not registered error 应用被杀死后,某些Android手机中的小部件无法在AppWidgetProvider中接收广播 - Widget in some android phone can not receive broadcast in AppWidgetProvider when app has been killed android-如何广播接收器是否已注册? - android - how to broadcast receiver is registered or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM