我的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

我的Android进阶之旅------>如何解决Android 5.0中出现的警告: Service Intent must be explicit:的更多相关文章

  1. 【我的Android进阶之旅】解决Android Studio启动时报错:Java 1.8 or later is required.

    错误描述 在公司电脑上运行Android Studio 2.2已经有一段时间了,但是自己的笔记本上还是用的Android Studio 1.5,今天晚上下了一个Android Studio 2.2压缩 ...

  2. 【我的Android进阶之旅】解决Android Studio 运行gradle命令时报错: 错误: 编码GBK的不可映射字符

    1.问题描述 最近在负责公司基础业务和移动基础设施的开发工作,正在负责Lint代码静态检查工作.因此编写了自定义的Lint规则,在调试过程中,编译的时候出现了如下所示的错误: 部分输出日志如下所示: ...

  3. 【我的Android进阶之旅】解决错误:No enum constant com.android.build.gradle.OptionalCompilationStep.FULL_APK

    今天在分支编译代码并允许之后,接着同步主干代码之后,再继续点击[Run]按钮允许程序的时候报错了,错误描述日志如下所示: 一.错误描述 Error:(1, 1) A problem occurred ...

  4. 【我的Android进阶之旅】解决bug:You need to use a Theme.AppCompat theme (or descendant) with this activity.

    前言 今天用Android Studio 生成Activity的时候,默认继承AppCompatActivity ,而在AndroidManifest.xml我对该Activity设置了一个主题,然后 ...

  5. 我的Android进阶之旅------>解决:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.

    错误描写叙述 今天在Android Studio项目中添加了jackson的开发包,编译执行时候.引发了例如以下的错误: Error:Execution failed for task ':app:t ...

  6. 【我的Android进阶之旅】解决sqlcipher库:java.lang.IllegalStateException: get field slot from row 0 col 0 failed.

    一.背景 最近维护公司的大数据SDK,在大数据SDK里面加入了ANR的监控功能,并将ANR的相关信息通过大数据埋点的方式记录到了数据库中,然后大数据上报的时候上报到大数据平台,这样就可以实现ANR性能 ...

  7. 我的Android进阶之旅------>如何在多个LinearLayout中添加分隔线

    如果要适合于所有的Android版本,可以在多个LinearLayout放置用于显示分隔线的View.例如,放一个ImageView组件,然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码如 ...

  8. 【我的Android进阶之旅】解决AndroidStudio编译时报错:Timeout waiting to lock artifact cache .

    1. 错误描述 今天在Android Studio中,使用gradle命令的时候,出现了如下所示的错误: D:\GitLab Source\XTCLint>gradlew clean uploa ...

  9. 【我的Android进阶之旅】解决Center OS 64位系统编译Android APP报错error=2和finished with non-zero exit value 127

    一.错误描述 1.问题 java.io.IOException: error=2, 没有那个文件或目录 今天在刚重新搭建好的64位的Center OS上安装好了Android SDK,Jenkins, ...

随机推荐

  1. TCP/IP各层协议数据格式

    ISO规范里定义了7层网络模型,实际常用的仍为TCPIP四层网络模型. 注:本文章插图均来自<图解TCP/IP>. 数据链路层帧格式 经常说的帧格式为以太网帧格式,由于类型和帧长度字段不重 ...

  2. [转]用了docker是否还有必要使用openstack?

    从一项颠覆性的技术成果转化并衍生出一整套社区体系,Docker在发展速度上打破了一个又一个历史纪录.然而,Docker项目在采纳与普及方面表现出惊人态势的同时,也给我们带来了一系列疑问与困惑. 在今天 ...

  3. liunx下安装mysql(未完待更新)

    1.下载mysql-liunx 下载地址:http://download.csdn.net/download/yichen01010/10019139 2.删除系统自带mysql rpm -qa|gr ...

  4. 什么是 AJAX ?

    什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味 ...

  5. ar0331

    aptina公司出品 分三个文档:ar0331_ds.pdf 数据表ar0331_dg.pdf 开发指导ar0331_rr_d.pdf 相关寄存器 色调映射(Tone Mapping):http:// ...

  6. 【翻译自mos文章】使用asm来部署 超大数据库(10TB到PB 范围)--针对oracle 10G

    使用asm来部署 超大数据库(10TB到PB 范围) 參考原文: Deployment of very large databases (10TB to PB range) with Automati ...

  7. redis 命令行 操作

    redis目前提供四种数据类型:string,list,set及zset(sorted set). * string是最简单的类型,你可以理解成与Memcached一模一个的类型,一个key对应一个v ...

  8. Windows下RabbitMQ安装,部署,配置

    安装部署 1.当前环境以及参考资料出处 部署环境:windows server 2008 r2 enterprise 官方安装部署文档:http://www.rabbitmq.com/install- ...

  9. ROS导航之参数配置和自适应蒙特卡罗定位

    我们的机器人使用两种导航算法在地图中移动:全局导航(global)和局部导航(local).这些导航算法通过代价地图来处理地图中的各种信息,导航stack使用两种costmaps http://www ...

  10. thrift框架总结,可伸缩的跨语言服务开发框架

    thrift框架总结,可伸缩的跨语言服务开发框架 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等.其 ...