gpt4 book ai didi

java - 如何在从 Android 服务类启动其他 Activity 后关闭 1 个 Activity

转载 作者:行者123 更新时间:2023-12-01 11:15:12 25 4
gpt4 key购买 nike

我创建了一项服务,该服务在收到来自某些号码的 SMS 后启动 Activity。

有 2 个 Activity 成功失败

两个 Activity 仅成功打开一次,在第二次迭代时,当我再次发送短信时,它不会影响 Activity 屏幕。

它保持当前 Activity 屏幕的打开状态,并且不会根据条件更改它。

我在网上搜索了这个并找到了各种解决方案,但这里似乎没有任何效果,我尝试更改标志,但它只允许服务类中的 1 个标志,如果我选择其他 Flags 它会给出我遵循错误消息。

从 Activity 上下文外部启动 Activity 需要 FLAG_ACTIVITY_NEW_TASK 标志

这是我写的服务类。请检查这一点并指导我在这里做错了什么。

谢谢

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);
}

最佳答案

尝试用以下代码替换您的代码:

 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);
}

关于java - 如何在从 Android 服务类启动其他 Activity 后关闭 1 个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31914600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com