首页 > 系统 > Android > 正文

Android5.0后服务禁止使用隐式启动

2019-11-08 00:00:34
字体:
来源:转载
供稿:网友

Android 5.0之后禁止使用隐式启动服务

这段时间维护一段老代码,发现在低版本上的手机上没问题,在5.0之后的手机出现下列错误: Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.xxx.xxx.xxx.xxx (has extras) } 可是我去配置文件中看的话明明是定义了这个服务的Action呀,最后找了半天原来是5.0之后服务不能以前那样直接隐式启动,需要指定Intent的setPackage(“包名”),或者指定Intent的ComponentName信息:intent.setComponent(xxx).

两种方法如下

方法一:Intent serviceIntent = new Intent();serviceIntent.setActio("你的服务action");serviceIntent.setPackage(你的应用的包名");context.startService(serviceIntent);方法二Intent serviceIntent = new Intent();ComponentName componentName = new ComponentName(你的应用的包名,serviceName);serviceIntent.setComponent(componentName);context.startService(serviceIntent);

在这里记录一下以便后面遇到了这个问题可以查询.


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表