android5.0之后不允许使用隐式Intent启动Service
来源:互联网 发布:usb转网络接口装驱动 编辑:程序博客网 时间:2024/06/05 02:43
android5.0以后不能使用隐式intent启动Service,即不允许只设置action的方式来启动service:
// 实例化Intent Intent it = new Intent(); //设置Intent的Action属性 it.setAction("com.android.activity.MY_ACTION"); // 启动Activity startActivity(it);
需要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage(“包名”),如果两者都没有指定的话将会报以上错误。尤其在framework层启动APP层的service时,如果是隐式启动service,可能会导致系统进程挂掉,出现不断重启的现象。
那么解决方法可以分为以下几种:
有包名和Service类名:
Intent intent = new Intent();ComponentName componentName = new ComponentName(pkgName,serviceName);intent.setComponent(componentName);context.startService(intent);
有包名,并且有Service设置的intent-filter中的action:
Intent mIntent = new Intent();mIntent.setAction("XXX.XXX.XXX");//Service能够匹配的ActionmIntent.setPackage(pkgName);//应用的包名context.startService(mIntent);......<intent-filter> <action android:name="android.intent.action.TestAIDLService"/> <category android:name="android.intent.category.DEFAULT"/></intent-filter>
只有intent-filter的action属性:
public static Intent getTempIntent(Context context, Intent implicitIntent) { // Retrieve all services that can match the given intent PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); // Make sure only one match was found if (resolveInfo == null || resolveInfo.size() != 1) { return null; } // Get component info and create ComponentName ResolveInfo serviceInfo = resolveInfo.get(0); String packageName = serviceInfo.serviceInfo.packageName; String className = serviceInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); // Create a new intent. Use the old one for extras and such reuse Intent explicitIntent = new Intent(implicitIntent); // Set the component to be explicit explicitIntent.setComponent(component); return explicitIntent; }......Intent intent = new Intent();intent.setAction("android.intent.action.TestAIDLService");Intent eintent = new Intent(getTempIntent(this,intent));bindService(eintent,mConnection,Context.BIND_AUTO_CREATE);
阅读全文
1 0
- android5.0之后不允许使用隐式Intent启动Service
- Android 5.0之后使用隐式Intent方式启动Service
- 关于Android5.0当中的Service启动问题--- Service Intent must be explicit: Intent
- android5.0之后必须显示调用,否则报java.lang.IllegalArgumentException: Service Intent must be explicit
- 解决Android5.0及以上启动Service时,java.lang.IllegalArgumentException: Service Intent must be explicit
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- Android 5.0 禁止使用隐式Intent来启动Service.异常:service intent must be explicit
- Android5.0启动Service问题
- Android5.0启动Service注意事项
- Android5.0后服务禁止使用隐式启动
- Android 5.0之后禁止用隐式Intent启动Service
- getopt optarg
- Android 4.4.2引入的超炫动画库
- ACM-ICPC北京赛区网络赛2017-minimum(线段树)
- Tesseract-OCR学习系列(三)简例
- JDK1.7和1.8中List集合中sort方法排序问题【JAVA】
- android5.0之后不允许使用隐式Intent启动Service
- caffe学习日记--lesson3:win8系统,VS2013下的caffe工程编译探究
- ArcGIS for Android Runtime100 基本操作(六)——移动地图包MMPK的使用
- PL/SQL备份数据库详解
- 【转】Web测试中的界面测试用例设计
- 【安卓自带数据库的简单应用】自制账号集APP
- Kotlin环境篇
- Hadoop for .NET Developers(十二):实现简单的MapReduce作业
- SVM为什么走下“神坛”?