問題描述:
java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.xk.file.FIRST_SERVICE }
Service啟動的方式有兩種:顯式啟動、隐式啟動。
1、隐式啟動
在AndroidManifest.xml注冊時需要添加指定的action。
<service android:name=".service">
<intent-filer>
<action android:name="com.android.service"/>
<intent-filer>
</service>
代碼中調用時采用:
Intent serviceIntent=new Intent();
serviceIntent.setAction("com.android.service");
2、顯示啟動
如果在同一個包中。兩者都可以用。在不同包時。隻能用隐式啟動。
Intent serviceIntent=new Intent(this,service.class);
startService(serviceIntent);
但是在安卓5.0時候,采用隐式啟動時,會出現java.lang.IllegalArgumentException: Service Intent must be explicit異常。也就是說Service的Intent必須明确。
解決方法就是給Intent設定一下具體的包名,指明具體是哪個包啟動的Service。
例如:
Intent serviceIntent=new Intent();
serviceIntent.setPackage("com.android.vending")
serviceIntent.setAction("com.android.service");
startService(serviceIntent);