简体   繁体   English

Android在下次应用加载时取消注册广播接收器

[英]Android unregister broadcast receiver on next app load

I have a feeling i already know the answer to this but im not sure. 我有一种感觉,我已经知道答案,但我不确定。 Im using a broadcast receiver to intercept incoming SMS messages. 我使用广播接收器拦截传入的SMS消息。

I register the receiver the typical way using the registerReceiver function and when you unregister the receiver you use the function unregisterReceiver function. 我使用registerReceiver函数以典型方式注册接收器,当您取消注册接收器时,使用函数unregisterReceiver函数。 I keep a global broadcast variable to load and unload the receiver as needed. 我保留一个全局广播变量来根据需要加载和卸载接收器。

If the app closes, like the user actually closes the application and i don't unregister the receiver in the onDestroy method which i know is bad practice, would there be a way to unregister the receiver the next time the app loads? 如果应用程序关闭,就像用户实际关闭应用程序而我没有在onDestroy方法中取消注册接收器,我知道这是不好的做法,是否有办法在下次应用程序加载时取消注册接收器? Could i create another instance of that broadcast receiver and then unregister it? 我可以创建该广播接收器的另一个实例,然后取消注册吗?

If the app closes 如果应用关闭

You can say that your UI is in the foreground or not. 您可以说您的UI处于前台。 And you can say that your app's process is in the foreground, the background, or terminated. 您可以说您的应用程序的进程位于前台,后台或终止。

It is unclear what "closing" would equate to. 目前还不清楚“结束”会等同于什么。

like the user actually closes the application 就像用户实际关闭应用程序一样

A user can move an app's UI and process to the background (eg, press HOME). 用户可以将应用程序的UI移动到后台(例如,按HOME)。 A user can destroy an app's UI and move its process to the background (eg, press BACK from the last running activity). 用户可以销毁应用程序的UI并将其进程移至后台(例如,从上次运行的活动中按BACK)。 A user can terminate an app's background process (eg, swipe the app off of the recent-tasks list). 用户可以终止应用程序的后台进程(例如,从最近任务列表中滑动应用程序)。 A user can force-stop an app (eg, pressing the Force Stop button for the app in Settings). 用户可以强制停止应用程序(例如,在“设置”中按下应用程序的“强制停止”按钮)。

It is unclear what "the user actually closes the application" equates to. 目前还不清楚“用户实际关闭应用程序”等同于什么。

would there be a way to unregister the receiver the next time the app loads? 有没有办法在下次加载应用程序时取消注册接收器?

Either you have a reference to your BroadcastReceiver object, or you do not. 您可以引用BroadcastReceiver对象,也可以不引用。 If you do, call unregisterReceiver() on some Context , passing in that BroadcastReceiver instance. 如果这样做,请在某个Context上调用unregisterReceiver() ,并传入该BroadcastReceiver实例。 If you do not have the BroadcastReceiver instance, you cannot unregister the receiver. 如果您没有BroadcastReceiver实例,则无法取消注册接收器。 If your process had been terminated between when you registered the receiver and now, that receiver is gone and is effectively unregistered. 如果您的注册接收器之间的过程已经终止,那么该接收器已经消失并且实际上未注册。

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

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