在 Service 的背景執行緒中建立 Intent、放入 Bundle 並啟動 Activity,但在目標 Activity 的 onCreate 中呼叫 getExtras() 卻得到 null。若將相同代碼移至 onStartCommand 則正常運作。
intent.setAction("唯一動作字串"),且接收端 AndroidManifest.xml 的 <intent-filter> 必須宣告相同 Action。java.io.Serializable 或 Parcelable 介面,否則序列化失敗導致資料遺失。TransactionTooLargeException。// Service 執行緒中
Intent intent = new Intent(this, TargetActivity.class);
intent.setAction("com.example.ACTION_START"); // 必須設定
Bundle bundle = new Bundle();
bundle.putString("DevID", alarmInfo.getDevId()); // 確保字串可序列化
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 非 Activity 啟動需加此旗標
startActivity(intent);
<action android:name="com.example.ACTION_START"/>