我的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. flink on yarn部分源码解析 (FLIP-6 new mode)

    我们在https://www.cnblogs.com/dongxiao-yang/p/9403427.html文章里分析了flink提交single job到yarn集群上的代码,flink在1.5版 ...

  2. 3. Digit Counts【medium】

    Count the number of k's between 0 and n. k can be 0 - 9.   Example if n = 12, k = 1 in [0, 1, 2, 3, ...

  3. 简单讲一下 SpringMVC的执行流程?

    执行流程: 1. 用户向服务器发送请求,请求被 Spring 前端控制 Servelt DispatcherServlet 捕获(捕获) . DispatcherServlet对请求  URL进行解析 ...

  4. SQL练习题汇总(Sqlserver和Mysql版本)

    所需表及数据执行脚本: CREATE TABLE STUDENT (SNO ) NOT NULL, SNAME ) NOT NULL, SSEX ) NOT NULL, SBIRTHDAY DATET ...

  5. Linux之IO Redirection

    一.引言 前几天使用一个linux下的内存检测工具valgrind,想要把检测的结果重定向到文件,结果总是没有任何内容,最后才发现是重定向的原因,它输出的信息是输出到stderr的,所以我使用 > ...

  6. logback.xml

    <?xml version="1.0" encoding="UTF-8" ?> <configuration> <!-- 日志存放 ...

  7. cocos2d-JS (二)Cocos Creater

    我觉得我们开发使用最新的工具.这对于我们非常有帮助. 由于新的工具有着新的功能,简化了我们开发的模式. 站在巨人的肩膀上~~~~~~~~~~~~~ CocosCreator.最大的两个特点   组件化 ...

  8. OpenCV3.1.0+VS2013配置+Win10(64位)(转载)

    OpenCV3.1.0+VS2013配置+Win10(64位) [环境]VS2013和MATLAB相互调用混合编程 Matlab 2016a和VS2013混合Dll编程步骤 更换了硬盘之后,重新配置了 ...

  9. Django1.6 +wsgi 部署到Apache2 的步骤。

    网上很多教程都是关于1.6之前的版本,很多都不适用,经历告诉我们最靠谱的还是官方文档. 一个Demo例子: 以 python shell开发的方式部署没有问题,但当独立部署到Apache2的过程非常艰 ...

  10. 最短路中部分点仅仅能从中随意选取K个问题

    题意:给N个点,还有另外m个点(当中仅仅能选K个).求最短路. 思路:在SPFA的基础上,用一个数组来统计,在某点入队时(要拓展其它点了),若该点是m个点中的,则count[i]=原来的+1:若不是. ...