Android中在service中启动activity

启动activity:

Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);

报错:android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

正确方式:

Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);-----重要
startActivity(intent);

说明:启动的activity需要在AndroidManifest.xml中配置此activity的theme

<activity 
    android:name=".MainActivity"
    android:theme="@style/AppTheme"/>
发布了19 篇原创文章 · 获赞 15 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/aidou1314/article/details/90700807