Android 中止广播接收器
我试图在退出应用程序后阻止 BroadcastReceiver 显示。到目前为止,我只让它在安装应用程序时显示 Toast。它工作得非常好,除了如果我退出应用程序接收器仍然处于活动状态。 这是来自 AndroidManifest 的接收器代码:
<receiver android:name=".MyBrowdcastreceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<data android:scheme="package" />
</intent-filter>
</receiver>
我想知道应该在主 Activity 的 onDestroy() 或 onStop() 方法中添加哪些内容,这将导致接收器停止。
谢谢。
I am trying to stop a BroadcastReceiver from showing up once I exit my application. Until now I only made it show a Toast when an App is installed. It works really well, except that if I exit the app the receiver is still active.
This is my Receiver code from the AndroidManifest:
<receiver android:name=".MyBrowdcastreceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<data android:scheme="package" />
</intent-filter>
</receiver>
I want to know what should I put in my onDestroy() or onStop() methods from my main Activity that will cause the receiver to stop.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已在清单文件中注册了接收器,即使您的应用程序未运行,该接收器也会收到通知。您可以执行两件事来满足您的需求:
您需要以编程方式注册、取消注册接收器。
您可以采用布尔值,如果应用程序正在运行,则将其设置为 true,否则设置为 false。您可以在 BraodcastReceiver 的 onReceive() 方法上使用此布尔值,您需要检查该布尔值,并在布尔值为 true 时执行操作。
You have registered the receiver in manifest file which will receive notification even if your app is not running.You can do two things to fulfill your need:
You need to register , unregister the receiver programmatically.
You can take boolean which you will set true if the app is running else false. You can use this boolean on BraodcastReceiver's onReceive() method where you need to put check over the boolean and perform your action if the boolean is true.