• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > 我的Android进阶之旅------Android 5.0中出现警告的解决方法: Service Intent must be explicit:

我的Android进阶之旅------Android 5.0中出现警告的解决方法: Service Intent must be explicit:

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了我的Android进阶之旅------Android 5.0中出现警告的解决方法: Service Intent must be explicit:等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

我的Android进阶之旅------Android 5.0中出现警告的解决方法: Service Intent must be explicit:


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:

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

这里写图片描述

以上代码就是加了一行<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;"> //不加这句话的话 android 5.0以上会报:Service Intent must be explitict intent.setPackage(getPackageName());

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

网站的截图,如下所示:

google官方网站上的提示

 
 </div>  </div>

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2017-05-26linux 内存性能调优
  • 2017-05-26Android 框架启动流程
  • 2017-05-26我的android学习经历3,android学习经历3
  • 2018-01-28Android广播机制
  • 2017-05-26安卓开发—简单的登陆界面,安卓登陆界面
  • 2017-05-26eclipse安装genymotion插件。,eclipsegenymotion
  • 2017-05-26Android中事件的分发机制
  • 2017-05-26Android 实用代码片段,
  • 2017-05-26内核内存分配常用函数使用
  • 2017-05-26机顶盒上gridview+ScrollView的使用。,gridviewscrollview

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Android 四大组件之Activity(续2),androidactivity
    • android 通过uri获取bitmap图片并压缩,
    • Android的Kotlin秘方(I):OnGlobalLayoutListener,
    • 【原】tinker dex文件格式的dump工具tinker-dex-dump,dextinker-dex-dump
    • ViewPager与PagerAdapter,viewpager
    • Android小项目:计算器
    • Android Studio 快捷键
    • 微博API常用方法,博API常用方法
    • android实现文字渐变效果和歌词进度的效果
    • React Native控件之PullToRefreshViewAndroid下拉刷新组件讲解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有