我的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, ...
随机推荐
- makefile之强制目标
强制目标 1. 定义 如果一个规则(rule_A)既没有依赖也没有命令,仅有目标(Targe_A),并且目标名不冲突.那么,在执行这个规则的时候,目标总被认为是更新过的.如果这个目标(Target_A ...
- python学习之sys模块
查看python的版本 >>> sys.version_info[] sys.argv 列表对象,传入模块参数的都会放入列表中. #-*- coding: utf-8 -*- # i ...
- 李洪强和你一起学习前端之(1)Html基础
1 快捷键的认识(虽然我用的是MAC,但是这里以windows快捷键来讲,但是MAC电脑可以把Ctrl换成command试试) Ctrl + c 复制 Ctrl + v 粘贴 Ctrl + a ...
- Jquery学习笔记(8)--京东导航菜单(2)增加弹框
京东导航,添加中间的弹框栏,使用position定位,放在左侧栏的li标签里面,成为一个整体,保证鼠标在弹框里的时候,弹框不消失: <!DOCTYPE html> <html lan ...
- SQL Server RAISERROR() 函数
生成错误消息并启动会话的错误处理. RAISERROR 可以引用 sys.messages 目录视图中存储的用户定义消息,也可以动态建立消息. 该消息作为服务器错误消息返回到调用应用程序,或返回到 T ...
- git上解决代码冲突(merge版)
1.切换到master: git checkout master 2.拉最新代码:git pull origin master 3.切换到提交的分支:git checkout Txxxx 4.合并:g ...
- mybatis 入门学习
所须要jar包:mybatis-3.x.x.jar .假设须要和spring整合,还须要增加相关的包 1:看项目文件夹 红颜色不要关心 2:依照步骤: 1:增加jar包 2:创建数据源(configu ...
- VS中常用的环境变量
环境变量名 含义 $(SolutionDir) 解决方案目录:即.sln文件所在路径 $(ProjectDir) 项目根目录:, 即.vcxproj文件所在路径 $(Configuration) 当前 ...
- Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0下面)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40411921.本文出自:[张鸿洋的博客] 1.概述 之前写过一篇博文:Andro ...
- 如何下载ubuntu桌面,并使用
下载ubuntu,进行linux系统的操作 1.下载ubuntu 百度搜索ubuntu或直达下载链接http://cn.ubuntu.com/download/ 你可以选择,优麒麟16或者Ubuntu ...