在Activity中可以直接使用Intent啟動另一個Activity
顯式Intent intent = new Intent(context, activity.class)
隱式 Intent intent = new Intent(“com.aa.www.act”);
startActivity(intent);
如果從Service中用同樣方法啟動Activity 的話,會報錯:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAGACTIVITYNEWTASK flag. Is this really what you want?
需要設置 intent.setFlags(Intent.FLAGACTIVITYNEWTASK); 再開啟activity 代碼如下 :
Intent intent = new Intent("hahaha.hahaha");
intent.setFlags(Intent.FLAGACTIVITYNEW_TASK);
startActivity(intent);