android5.0以后不能使用隐式intent启动service

转载自:https://www.cnblogs.com/xiaoxiaing/p/6278996.html

android5.0以后不能使用隐式intent :需要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage("包名"),如果两者都没有指定的话将会报以上错误。尤其在framework层启动APP层的service时,如果是隐式启动service,可能会导致系统进程挂掉,出现不断重启的现象。

三 解决方法

 1. Intent intent = new Intent();
    ComponentName componentName = new ComponentName(pkgName,serviceName);
    intent.setComponent(componentName);
    context.startService(intent);

  

2.Intent mIntent = new Intent();
 mIntent.setAction("XXX.XXX.XXX");//Service能够匹配的Action
 mIntent.setPackage(pkgName);//应用的包名
 context.startService(mIntent);

猜你喜欢

转载自blog.csdn.net/qq_28852011/article/details/80083880