首页 技术 正文
技术 2022年11月21日
0 收藏 682 点赞 2,253 浏览 5116 个字

我的Android进阶之旅——>怎样解决Android 5.0中出现的警告: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.xtc.kuwo.watch.MUSIC_PLAY_SERVICE (has extras) }

1.错误描写叙述

今天在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);

2.错误原因

有些时候我们使用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)方法

startService(Intent service)方法代码例如以下

 @Override
public ComponentName startService(Intent service) {
warnIfCallingFromSystemProcess();
return startServiceCommon(service, mUser);
}

startServiceCommon(Intent service, UserHandle user)方法

上面的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;
}
}

validateServiceIntent(Intent service)方法

上面的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:

版权声明:本文为【欧阳鹏】原创文章。欢迎转载,转载请注明出处!

http://blog.csdn.net/ouyang_peng/article/details/50727693

3.解决方法

设置要启动Service的Intent的Action和packageName

        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);

以上代码就是加了一行

        //不加这句话的话 android 5.0以上会报:Service Intent must be explitict
intent.setPackage(getPackageName());

此方式是google官方推荐使用的解决方法。

在此附上地址供大家參考:http://developer.android.com/goo … tml#billing-service,有兴趣的能够去看看。

以下是http://developer.android.com/goo … tml#billing-service站点的截图。例如以下所看到的:

版权声明:本文为【欧阳鹏】原创文章,欢迎转载,转载请注明出处!

http://blog.csdn.net/ouyang_peng/article/details/50727693

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉。

转载请保留原文地址:http://blog.csdn.net/ouyang_peng

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893