編輯:關於Android編程
今天在Android4.4 的小米4手機上運行我的程序的時候沒有報錯,而在Android 5.1的華為P7上運行我的程序的時候報了以下的錯誤,錯誤提示如下:
E/AndroidRuntime(12500): FATAL EXCEPTION: main
E/AndroidRuntime(12500): Process: com.xtc.watch, PID: 12500
E/AndroidRuntime(12500): java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.xtc.kuwo.watch.MUSIC_PLAY_SERVICE (has extras) }
E/AndroidRuntime(12500): at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1847)
E/AndroidRuntime(12500): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1876)
E/AndroidRuntime(12500): at android.app.ContextImpl.startService(ContextImpl.java:1860)
E/AndroidRuntime(12500): at android.content.ContextWrapper.startService(ContextWrapper.java:516)
E/AndroidRuntime(12500): at com.xtc.watch.kuwo.activity.WatchMusicPlay.pauseMusic(WatchMusicPlay.java:314)
E/AndroidRuntime(12500): at com.xtc.watch.kuwo.activity.WatchMusicPlay.access$600(WatchMusicPlay.java:32)
E/AndroidRuntime(12500): at com.xtc.watch.kuwo.activity.WatchMusicPlay$3.onClick(WatchMusicPlay.java:220)
E/AndroidRuntime(12500): at android.view.View.performClick(View.java:4790)
E/AndroidRuntime(12500): at android.view.View$PerformClick.run(View.java:19933)
E/AndroidRuntime(12500): at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(12500): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(12500): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(12500): at android.app.ActivityThread.main(ActivityThread.java:5569)
E/AndroidRuntime(12500): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(12500): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(12500): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
E/AndroidRuntime(12500): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
而我啟動Service的Intent代碼如下所示:
Intent intent = new Intent();
intent.setAction(MUSIC_PLAY_SERVICE);
intent.putExtra("MSG", Constants.PlayerMsg.PAUSE_MSG); //暫停播放音樂
intent.putExtra("musicURL", musicURL); //歌曲URL
startService(intent);
有些時候我們使用Service的時需要采用隱私啟動的方式,但是Android 5.0一出來後,其中有個特性就是Service Intent must be explitict,也就是說從Android Lollipop版本(Android 5.0)開始,service服務必須采用顯示方式啟動。
而android源碼是這樣寫的(源碼位置:sdk/sources/android-21/android/app/ContextImpl.java):
startService(Intent service)方法代碼如下
@Override
public ComponentName startService(Intent service) {
warnIfCallingFromSystemProcess();
return startServiceCommon(service, mUser);
}
上面的startService(Intent service)方法調用的是startServiceCommon(Intent service, UserHandle user),代碼如下所示:
private ComponentName startServiceCommon(Intent service, UserHandle user) {
try {
validateServiceIntent(service);
service.prepareToLeaveProcess();
ComponentName cn = ActivityManagerNative.getDefault().startService(
mMainThread.getApplicationThread(), service,
service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
if (cn != null) {
if (cn.getPackageName().equals("!")) {
throw new SecurityException(
"Not allowed to start service " + service
+ " without permission " + cn.getClassName());
} else if (cn.getPackageName().equals("!!")) {
throw new SecurityException(
"Unable to start service " + service
+ ": " + cn.getClassName());
}
}
return cn;
} catch (RemoteException e) {
return null;
}
}
上面的startServiceCommon(Intent service, UserHandle user)方法中調用的validateServiceIntent(Intent service)方法代碼如下所示:
private void validateServiceIntent(Intent service) {
if (service.getComponent() == null && service.getPackage() == null) {
if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
IllegalArgumentException ex = new IllegalArgumentException(
"Service Intent must be explicit: " + service);
throw ex;
} else {
Log.w(TAG, "Implicit intents with startService are not safe: " + service
+ " " + Debug.getCallers(2, 3));
}
}
}
可以看得出來,就是在validateServiceIntent(Intent service)方法中判斷如果大於Build.VERSION_CODES.LOLLIPOP版本的話,並且啟動Service的Intent如果沒有設置Component和Package的話就會跑出異常java.lang.IllegalArgumentException: Service Intent must be explicit:
Intent intent = new Intent();
intent.setAction(MUSIC_PLAY_SERVICE);
intent.putExtra("MSG", Constants.PlayerMsg.PAUSE_MSG); //暫停播放音樂
intent.putExtra("musicURL", musicURL); //歌曲URL
startService(intent);
改為:
Intent intent = new Intent();
intent.setAction(MUSIC_PLAY_SERVICE);
//不加這句話的話 android 5.0以上會報:Service Intent must be explitict
intent.setPackage(getPackageName());
intent.putExtra("MSG", Constants.PlayerMsg.PAUSE_MSG); //暫停播放音樂
intent.putExtra("musicURL", musicURL); //歌曲URL
startService(intent);
以上代碼就是加了一行<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">
//不加這句話的話 android 5.0以上會報:Service Intent must be explitict
intent.setPackage(getPackageName());
此方式是google官方推薦使用的解決方法。
網站的截圖,如下所示:
BottomNavigationView 很早之前就在 Material Design 中出現了,但是直到 Android Support Library 25 中才增加
一.效果展示在Android手機的桌面上,我們經常可以看到如下小控件在這些控件上,可以顯示我們APP的一些重要的交互信息,以我最近開發的手機衛士為例,在widget上可以
本文實例為大家分享了Android計時器的三種方法,具體內容如下目錄:1、借助Timer實現2、調用handler.sendMessagedely(Message msg
1 引入如何高效地實現以下界面?public class User { private String name; private int score;