我有一个Action类,在接收SMS时被调用。在该类的内部,我调用一个活动并执行一些必需的操作。
public class SMSReceiver extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public static boolean wasScreenOn = true;
Context context;
public void onReceive(Context context, Intent intent) {
this.context = context;
// Intent intent = new Intent();
// starting activity and performing some other action
}
}
// part of AndroidManifest.xml
<receiver android:name="com.**************.********.SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
我面临的问题是当我在Activity类并执行某些操作时,同时如果我收到另一条消息,则调用主动作类(SMSReceiver)。 我想要的是等待BraoadCast接收器,直到我完成一项活动并完成该活动。
我是android和stackoverflow的新手。对不起如果我没有遵守规则(至少我遵循1条规则,我在询问这个问题之前通过搜索得到了答案)或者我没有正确地提出问题
答案 0 :(得分:2)
在SMSReceiver中有一个静态标志isProcessingMessage = false。每当调用onReceive时,将其设置为true并调用您的Activity。
现在为您自己创建的接收者注册SMSReceiver说“sms_processing_finished”并将其添加到SMSReceiver的 intent-filter 。 &lt; ------别忘了!!
因此,当您的活动处理结束时,生成一个广告接收器,其动作为“sms_processing_finished”。
private void sendSmsProcessingOver() {
Intent intent = new Intent("sms_processing_finished");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
现在在OnReceive
public void onReceive(Context context, Intent intent) {
this.context = context;
String action = intent.getAction();
if(action.equals("sms_processing_finished"){
isProcessingMessage = false;
/* Now check if any new messages that has arrived is in queue or not.
if there are messages in the queue, dequeue them and launch your activity */
}
else{
if(isProcessingMessage){
/* Queue the Message so that when the processing is over, it will dequeue when
it receives "sms_processing_finished" broadcast. */
return;
}
/*do your normal stuff here*/
}
}