简体   繁体   English

即使在应用崩溃时,广播接收器如何正常工作

[英]How a broadcast receiver can function normally even on app crash

Problem scenario: I have an android application with a statically registered broadcast receiver having intent filter registered for AIRPLANE_MODE is working fine. 问题场景:我有一个带有静态注册的广播接收器的android应用程序,该广播接收器的AIRPLANE_MODE注册的意图过滤器工作正常。 When I turn on the airplane mode, a toast is displayed. 当我打开飞行模式时,将显示吐司。 When I force stop my application, the broadcast receiver doesn't work which is an expected result. 当我强制停止我的应用程序时,广播接收器不起作用,这是预期的结果。 But now when my app crashes, still the broadcast receiver is displaying toast. 但是现在当我的应用崩溃时,广播接收器仍然显示吐司。 So, my question is why my broadcast receiver is working even on app crash whereas it is destroyed when I force stop the application. 所以,我的问题是,为什么我的广播接收器即使在应用崩溃时也能正常工作,而当我强制停止应用时却被破坏了。 It will be really helpful if someone could provide a clear understanding of App Force Stop scenario. 如果有人可以清楚地了解App Force Stop方案,这将非常有帮助。

In your app, you have 4 points for starting your app: BroadcastReceiver , Activity , Service , ContentProvider . 在您的应用程序中,您有4个启动应用程序的点: BroadcastReceiverActivityServiceContentProvider

So if you register your BroadcastReceiver in AndroidManifest, even if u make a force stop app, you will actually getting callbacks in BroadcastReceiver, because it's a point of starting app. 因此,如果您在AndroidManifest中注册了BroadcastReceiver,即使您强制停止应用程序,实际上也会在BroadcastReceiver中获得回调,因为这是启动应用程序的关键。 You actually can disable BroadcastReceiver programatically by following code: 实际上,您可以通过以下代码以编程方式禁用BroadcastReceiver:

ComponentName component = new ComponentName(context, YourReceiver.class); 

//Disable BroadcastReceiver
context.getPackageManager().setComponentEnabledSetting(component,  PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);

//Enable BroadcastReceiver
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);

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

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