查找某个Service是否在运行与bindService隐性Intent有何不同,是否适应单通道
来源:互联网 发布:长春seo排名 编辑:程序博客网 时间:2024/05/17 02:17
通过隐性Intent绑定bindService是按照包来顺序查找的,即使现在没有一个应用运行这个service,也有可能轮不到自己的应用。
如果通过查找某个service是否在运行,再来绑定它,这时规则不同,会导致一台设备上会有两个Service进程,这与单通道应用相违背。
/**
* 把隐式Intent转为显式Intent 规则必须与bindService一样
* @param context
* @param implicitIntent
* @return
*/
private Intent createExplicitFromImplicitIntent(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() != 0) {
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;
}
上面这种通过在应用中查找的顺序就与bindService的顺序一样。
//查找运行的服务
private Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
Intent explicitIntent = new Intent(implicitIntent);
ActivityManager myAM = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> myList = myAM.getRunningServices(40);
if (myList.size() > 0) {
for (int i = 0; i < myList.size(); i++) {
String mName = myList.get(i).service.getClassName().toString();
if (mName.equals(PushService.class.getName())) {
String packageName = myList.get(i).service.getPackageName();
String className = mName;
ComponentName component = new ComponentName(packageName, className);
explicitIntent.setComponent(component);
return explicitIntent;
}
}
}
// Get component info and create ComponentName
String packageName = context.getPackageName();
String className = PushService.class.getName();
ComponentName component = new ComponentName(packageName, className);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
上面这种的话就不同。
所以安桌5.0上不能用隐性Intent来bindService时,只能用上面那种方法来实现单通道。
- 查找某个Service是否在运行与bindService隐性Intent有何不同,是否适应单通道
- Android 判断某个service是否在运行
- OpenUDID 是否足够替代 UDID 使用?有何不同?
- OpenUDID 是否足够替代 UDID 使用?有何不同?
- 关于通过bindService启动的service,在unbindService后service是否继续运行的讨论
- 关于通过bindService启动的service,在unbindService后service是否继续运行的讨论
- 多线程的程序在多核CPU与单核CPU运行效率有何不同
- 定时监控某个web端口服务是否有在运行。
- 判断某个service服务是否正在运行
- 判断某个service是否正在运行
- 查找某个数是否在矩阵中
- 查找某个类中是否有某个注解
- android 判断某个应用是否在运行
- android判断某个服务是否在运行
- 判断某个服务是否在运行
- linux 查看某个进程是否在运行
- Android判断某个服务是否在运行
- JavaBean与EJB有何不同
- HTTP 错误 500.21 - Internal Server Error处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipel
- getline和cin.getline
- 第十一周阅读程序7:virtual 虚拟函数
- CSS line-height 属性
- 21分钟 MySQL 入门教程(转载)
- 查找某个Service是否在运行与bindService隐性Intent有何不同,是否适应单通道
- tools:context=".MainActivity的作用
- AFNetworking 源码解释
- Happy Number
- SharePoint 2013 企业搜索架构示例
- properties导致空指针的问题
- 最小公倍数 UVa11889
- js函数调用的四种模式
- 使用Hadoop的datajoin包进行关系型join操作【hadoop关系型join,源码解析及如何应用jar包中的抽象类】