代碼中 BroadcastReceiver.onReceive 嘗試啟動 NofyService,但 Service 的 onCreate 未被調用。關鍵代碼:
Intent i = new Intent(context, NofyService.class);
i.addCategory(Intent.CATEGORY_DEFAULT); // 多餘且可能導致匹配失敗
context.startService(i);
addCategory:顯式 Intent(指定 Component)不需要 Category,添加後可能導致 Intent 與 Manifest 中 <service> 的 <intent-filter> 不匹配,系統找不到目標 Service。AndroidManifest.xml 中 Service 宣告包含 android:process 和 <intent-filter>,但顯式啟動不依賴 filter。context.startService(i) 返回值是否為 null(表示啟動失敗)。i.addCategory(...)。android:process),確保進程啟動權限無誤。Log.d("TAG", "startService result: " + context.startService(i)); 觀察返回組件名。顯式 Intent 啟動 Service 時,不要添加 Category,直接 new Intent(context, ServiceClass.class) 即可。