Reputation: 21
When I start activity from a BroadCastReceiver, the exception "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK" will happened. The follow is my receiver code
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);
}
}
You can see it ,I have set FLAG_ACTIVITY_NEW_TASK already. I want to point another thing. I send the broadcastreceiver from my HTTP request which in a work thread. The code most like this:
Handler mDelivery = new Handler(Looper.getMainLooper());
mDelivery.post(new Runnable() {
@Override
public void run() {
MyAppApplication.getInstance().sendBroadcast(new Intent(LogoutReceiver.LOGOUT_ACTION));
}
});
Anyone meet the problems? Any help is great.
Upvotes: 2
Views: 640
Reputation: 187
I am sorry for this question. My mistake cause this problems. Please have a look the point of problem.
@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
}
Sorry for this.
Upvotes: 1