1. 程式人生 > >(轉)如何在廣播接收器中開啟一個新的Activity

(轉)如何在廣播接收器中開啟一個新的Activity

原地址:http://blog.csdn.net/rually/article/details/47122937

在廣播接收器當中有Context 這個引數,說明是可以提供程式上下文環境引數的,但是如果我們直接用這樣的程式碼來開啟一個新的activity的話:

public void onReceive(Context context, Intent intent) {  
Intent intent2 = new Intent(context, AnotherActivity.class);  
context.startActivity(intent2);
會出現這樣的錯誤:


所以只需要新增一句話,就可以在Activity context 之外,開啟一個新的Activity了 

public void onReceive(Context context, Intent intent) {
Intent intent2 = new Intent(context, AnotherActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);