我创建了一项服务,在收到某些号码的SMS
后启动活动。
共有2项活动Success
和Failure
。
两个活动仅成功打开一次,在第二次迭代时,当我再次发送SMS
时,它不会影响活动屏幕。
它仍保持打开当前的“活动”屏幕,并且不会根据条件更改它。
我在网上搜索了这个并发现了各种解决方案,但似乎没有什么工作在这里,我试图更改标志,但它只允许服务类中的1个标志,如果我选择其他Flags
它会给我以下错误消息。
StartActivity from outsite an activity context requires the FLAG_ACTIVITY_NEW_TASK flag
这是我写的服务类。请检查一下,并指导我在这里做错了什么。
谢谢
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
int duration = Toast.LENGTH_LONG;
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
if(senderNum.equals("345"))
{
Intent successScreen= new Intent(context, SuccessActivity.class);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(successScreen);
}
else if(senderNum.equals("3450") || senderNum.equals("3451") || senderNum.equals("3452")){
Intent alertActivity = new Intent(context, FailureActivity.class);
alertActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alertActivity);
}
答案 0 :(得分:1)
尝试使用以下代码替换您的代码:
if(senderNum.equals("345"))
{
Intent successScreen= new Intent(context, SuccessActivity.class);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
successScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
context.startActivity(successScreen);
}
else if(senderNum.equals("3450") || senderNum.equals("3451") || senderNum.equals("3452")){
Intent alertActivity = new Intent(context, FailureActivity.class);
alertActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
successScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
context.startActivity(alertActivity);
}