startActivity
这里介绍从一个app显示调用另一个App的activity
Intent intent = new Intent();
if (true) { ComponentName cn = new ComponentName("com.example.test", "com.example.test.CurrentTestActivity"); } else { ComponentName cn = new ComponentName("com.example.test", "com.example.test.TestActivity"); }
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); intent.setComponent(cn); intent.putExtra("id", 12); intent.putExtra("duration", 1); intent.putExtra("times", 3); startActivity(intent);
需要注意的是 ComponentName第一个参数是组件名(通常是Android中声明的package)
如果调出的Activtivity只是一个功能片段,并没有实际的意义,也没有必要出现在长按Home键调出最近使用过的程序类表中,那么使用FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
被调用的app里面activity声明方式有两种方式,不过显然后面声明android:exported="true"的方式比较常用
<activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TestActivity" android:exported="true" > </activity>
Q: 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.FLAG_ACTIVITY_NEW_TASK

浙公网安备 33010602011771号