广播接收器组件不允许注册以接收意图 [英] BroadcastReceiver components are not allowed to register to receive intents
问题描述
我想倾听来自包含特定关键字的一个特定的发送方发送一个非常具体的短信。对于这一点,我创建了通过舱单结合 android.provider.Telephony.SMS_RECEIVED
A BroadcastReciever。
I am trying to listen to a very specific SMS sent from a particular sender containing a particular keyword. For that, I have created a BroadcastReciever which binds to android.provider.Telephony.SMS_RECEIVED
through manifest.
如果我发现某个发件人发送包含该关键字的短信,我需要从我的应用程序发送短信。我已经做了,在我的应用程序通过 onRecieve()
功能。
If I find that particular sender sending SMS containing that keyword, I need to send the SMS from my application. I have done that in my application through onRecieve()
function.
问题是,我要听SMS_SENT和SMS_DELIVERED事件,要知道,如果短信发送成功/交付与否。对于这一点,我是通过注册
The problem is that I want to listen to SMS_SENT and SMS_DELIVERED events to know that if sms was successfully sent/delivered or not. For that , I am registering these receivers through
context.registerReceiver(smsSentReciever, new IntentFilter(Consts.SENT));
context.registerReceiver(smsDeliveredReciver, new IntentFilter(Consts.DELIVERED));
虽然,我实例化一个单独的的AsyncTask
从 onRecieve
的方法来做好这项工作,但我仍然很下面让错误广播接收器组件不允许注册以接收意图
Though, I have instantiated a seperate AsyncTask
from the onRecieve
method to do this job, but still I am getting the error below
BroadcastReceiver components are not allowed to register to receive intents
我应该使用 IntentService
而不是的AsyncTask
从 onRecieve
?
或
Should I use IntentService
instead of AsyncTask
from the onRecieve
?
or
我应该实例化一个 IntentService
从的AsyncTask
在 onRecieve $ C $执行C>?
Should I instantiate an IntentService
from AsyncTask
executed in onRecieve
?
推荐答案
刚刚注册的BroadcastReceiver在你的清单来处理所有三种意图,并且因为他们收到分别处理它们。
Just register your BroadcastReceiver in your manifest to handle all three Intents, and deal with them separately as they are received.
在的AndroidManifest.xml
:
<receiver android:name=".YourBroadcastReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<intent-filter>
<action android:name="com.mycompany.myapp.SMS_SENT" />
<action android:name="com.mycompany.myapp.SMS_DELIVERED" />
</intent-filter>
</receiver>
在 YourBroadcastReceiver.java
:
public class YourBroadcastReceiver extends BroadcastReceiver
{
public static final String ACTION_SMS_RECEIVED =
"android.provider.Telephony.SMS_RECEIVED";
public static final String ACTION_SMS_SENT = "com.mycompany.myapp.SMS_SENT";
public static final String ACTION_SMS_DELIVERED = "com.mycompany.myapp.SMS_DELIVERED";
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(ACTION_SMS_RECEIVED))
{
}
// etc...
}
}
注: SMS_RECEIVED
优先级设置为999,所以我的应用程序可以其他应用程序,例如,平台SMS / MMS应用程序之前处理消息。
Note: The SMS_RECEIVED
priority is set to 999 so my app can handle the message before other apps, e.g., the platform SMS/MMS app.
这篇关于广播接收器组件不允许注册以接收意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!