设置的BroadcastReceiver是不活跃 [英] Set BroadcastReceiver to be inactive

查看:119
本文介绍了设置的BroadcastReceiver是不活跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BroadcastReceiver设立在我接收的短信接收事件的Andr​​oid应用程序。这工作得很好,但我希望能够切换短信接收和关闭通过切换BroadcastReceiver的开启和关闭。因为如果我有方法的onReceive里面一个简单的布尔,即使短信接收关闭,我的应用程序将启动。

I have a BroadcastReceiver set up in my Android application that receives SMS receive events. This works fine, but I want to be able to toggle SMS receiving on and off by toggling the BroadcastReceiver on and off. Because if I have a simple boolean inside the onReceive method, even if the SMS receiving is off, my application will start.

这是可能的吗?

干杯!

推荐答案

您可以使用<一个href="http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting%28android.content.ComponentName,%20int,%20int%29"相对=nofollow> PackageManager#setComponentEnabledSetting 启用/在你的清单文件中禁用的组件。您创建一个组件名与你的包的名字和你的广播接收器的类名。然后使用 COMPONENT_ENABLED_STATE_DISABLED 标记来禁用它。并根据如果你想整个应用程序上对象死亡或无法使用 DONT_KILL_APP 标记或0。虽然文档警告不要杀得了应用程序

You can use PackageManager#setComponentEnabledSetting to enable/disable a component in your manifest file. You create a ComponentName with your package name and class name of your broadcast receiver. Then use the COMPONENT_ENABLED_STATE_DISABLED flag to disable it. And depending on if you want the entire Application object to die or not use the DONT_KILL_APP flag or 0. Though the documentation warns against not killing the Application.

这篇关于设置的BroadcastReceiver是不活跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆