// 錯誤:第一個參數應為 Context,且無法跨包解析類
Intent intent = new Intent(getApplicationContext(), com.facebook.android.Places.class);
// Manifest 寫法也錯:點號開頭會被當作相對包名
<activity android:name=".com.facebook.android.Places"></activity>
Intent intent = new Intent();
intent.setClassName("com.facebook.android", "com.facebook.android.Places");
startActivity(intent);
intent.setComponent(new ComponentName("com.facebook.android", "com.facebook.android.Places"));
<!-- 目標 App Manifest -->
<activity android:name=".Places">
<intent-filter>
<action android:name="com.facebook.android.SHOW_PLACES" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
// 調用端
Intent intent = new Intent("com.facebook.android.SHOW_PLACES");
startActivity(intent);
<!-- 完整包名+類名,不加點號前綴 -->
<activity android:name="com.facebook.android.Places"
android:exported="true"> <!-- 必須 exported=true 允許外部啟動 -->
</activity>
| 現象 | 原因 | 解決 |
|---|---|---|
| ActivityNotFoundException | 包名/類名拼寫錯、目標未安裝、未 exported | adb shell pm list packages 確認包名;Android 12+ 必須 exported=true |
| SecurityException | 目標 Activity 權限保護 (android:permission) | 在調用端 Manifest 聲明 <uses-permission android:name="..."/> |
| 啟動到錯誤 Activity | 隱式 Intent 匹配多個 | 用顯式 Intent (setClassName/setComponent) |
跨包啟動**永遠用顯式 Intent (setClassName/setComponent)**,隱式 Intent 易受導出組件變更影響。目標 App 必須在 Manifest 顯式聲明
android:exported="true"(Android 12+ 強制)。