当前位置: 首页 > 知识库问答 >
问题:

已经设置了"从活动上下文外部调用启动活动()需要FLAG_ACTIVITY_NEW_TASK"

单于骁
2023-03-14

当我从BroadCastReceiver启动活动时,会出现异常“从活动上下文外部调用startActivity(),需要标记_activity_NEW_TASK”。下面是我的接收代码

public class LogoutReceiver extends BroadcastReceiver {
public static final String LOGOUT_ACTION = "com.ss.ee.logout";
private Logger logger = new Logger(LogoutReceiver.class.getSimpleName(), true);

@Override
public void onReceive(Context context, Intent intent) {

    Intent logoutIntent = new Intent(context, LoginActivity.class);
    logoutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK );
    //logoutIntent.putExtra("logout", true);
    context.startActivity(intent);
}

}

你可以看到,我已经设置了FLAG_ACTIVITY_NEW_TASK。我想指出另一件事。我从工作线程中的HTTP请求发送broadcastreceiver。代码最像这样:

Handler mDelivery = new Handler(Looper.getMainLooper());
mDelivery.post(new Runnable() {
            @Override
            public void run() {
                MyAppApplication.getInstance().sendBroadcast(new Intent(LogoutReceiver.LOGOUT_ACTION));
            }
        });

有人遇到问题吗?任何帮助都很好。

共有1个答案

苏鸿志
2023-03-14

我很抱歉问这个问题。我的错误导致了这些问题。请看一下问题所在。

@Override
public void onReceive(Context context, Intent intent) {

Intent logoutIntent = new Intent(context, LoginActivity.class);
logoutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK );
//logoutIntent.putExtra("logout", true);
context.startActivity(intent);//Here should be logoutIntent 
}

很抱歉。

 类似资料: