我的Android进阶之旅------>如何解决Android 5.0中出现的警告: Service Intent must be explicit:
我的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:的更多相关文章
- 【我的Android进阶之旅】解决Android Studio启动时报错:Java 1.8 or later is required.
错误描述 在公司电脑上运行Android Studio 2.2已经有一段时间了,但是自己的笔记本上还是用的Android Studio 1.5,今天晚上下了一个Android Studio 2.2压缩 ...
- 【我的Android进阶之旅】解决Android Studio 运行gradle命令时报错: 错误: 编码GBK的不可映射字符
1.问题描述 最近在负责公司基础业务和移动基础设施的开发工作,正在负责Lint代码静态检查工作.因此编写了自定义的Lint规则,在调试过程中,编译的时候出现了如下所示的错误: 部分输出日志如下所示: ...
- 【我的Android进阶之旅】解决错误:No enum constant com.android.build.gradle.OptionalCompilationStep.FULL_APK
今天在分支编译代码并允许之后,接着同步主干代码之后,再继续点击[Run]按钮允许程序的时候报错了,错误描述日志如下所示: 一.错误描述 Error:(1, 1) A problem occurred ...
- 【我的Android进阶之旅】解决bug:You need to use a Theme.AppCompat theme (or descendant) with this activity.
前言 今天用Android Studio 生成Activity的时候,默认继承AppCompatActivity ,而在AndroidManifest.xml我对该Activity设置了一个主题,然后 ...
- 我的Android进阶之旅------>解决:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
错误描写叙述 今天在Android Studio项目中添加了jackson的开发包,编译执行时候.引发了例如以下的错误: Error:Execution failed for task ':app:t ...
- 【我的Android进阶之旅】解决sqlcipher库:java.lang.IllegalStateException: get field slot from row 0 col 0 failed.
一.背景 最近维护公司的大数据SDK,在大数据SDK里面加入了ANR的监控功能,并将ANR的相关信息通过大数据埋点的方式记录到了数据库中,然后大数据上报的时候上报到大数据平台,这样就可以实现ANR性能 ...
- 我的Android进阶之旅------>如何在多个LinearLayout中添加分隔线
如果要适合于所有的Android版本,可以在多个LinearLayout放置用于显示分隔线的View.例如,放一个ImageView组件,然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码如 ...
- 【我的Android进阶之旅】解决AndroidStudio编译时报错:Timeout waiting to lock artifact cache .
1. 错误描述 今天在Android Studio中,使用gradle命令的时候,出现了如下所示的错误: D:\GitLab Source\XTCLint>gradlew clean uploa ...
- 【我的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, ...
随机推荐
- InnoDB:文件
Mysql中有多种类型的文件,每种类型的文件都有其特定的作用,下面就来说说: 参数文件:告诉Mysql实例数据库文件的位置,定义参数. 日志文件:Mysql实例对某种条件作出的响应写入文件,这个文件就 ...
- The Definitive Guide To Django 2 学习笔记(一) Views and UrL confsRL
1.如何找到django在Ubuntu下的安装路径: 进入python命令行,import django,print(django.__path__) 2.使用django-admin.py 创建项目 ...
- 数据库设计(三)11 important database designing rules which I follow
原文地址:https://www.codeproject.com/Articles/359654/important-database-designing-rules-which-I-fo 麻辣个
- IOS证书之Certificates,Devices, Identifiers & Profiles
做IOS开发的,在需要发布应用的时候,会接触到iOS Dev Center里面的证书制作,按照网上的资料操作,我们可以很容易的制作证书并且完成真机调试或者是产品发布,但是对于Certificates. ...
- 【Mac + Appium + Python3.6学习(五)】之常用的Android自动化测试API总结
Github测试样例地址:https://github.com/appium-boneyard/sample-code/tree/master/sample-code/examples ①定位text ...
- PAT003 List Leaves
题目: Given a tree, you are supposed to list all the leaves in the order of top down, and left to righ ...
- Data Collection
众所周知,计算机领域论文是要以实验为基础的,而实验的原料就是数据.不管是在图像,文字或者语音领域,开源的数据都十分宝贵和重要.这里主要收集各领域的一些常用的公开数据集. 计算机视觉: [ImageNe ...
- 05 Java图形化界面设计——布局管理器之GridLayout(网格布局)
网格布局特点: l 使容器中的各组件呈M行×N列的网格状分布. l 网格每列宽度相同,等于容器的宽度除以网格的列数. l 网格每行高度相同,等于容器的高度除以网格的行数. l 各组件的排列方式 ...
- Map Labeler (poj 2296 二分+2-SAT)
Language: Default Map Labeler Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1815 Ac ...
- Material design之New Widgets(RecyclerView CardView)
New Widgets:提供了两个新的控件 RecyclerView CardView 这两个控件包含在了Android L的support library中, 他们可以用于显示复杂的布局而且都默认采 ...