判断Activity通过Action
PackageManager packageManager = context.getPackageManager();
//指定要查找的Activity Action:com.android.phone.action.TOUCH_DIALER
Intent intent = new Intent("com.android.phone.action.TOUCH_DIALER");
//在系统中查询指定的Activity Action
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS);
//如果返回为0 表示没有找到
if(resolveInfos.size()==0){
Log.e("TAG", "没有安装");
}
判断Broadcast通过action
PackageManager packageManager = context.getPackageManager();
//指定要查找的Activity Action:com.android.action.MYBROADCAST
Intent intent = new Intent("com.android.action.MYBROADCAST");
//在系统中查询指定的BroadCast Action
List<ResolveInfo> resolveInfos = packageManager.queryBroadcastReceivers(intent, PackageManager.GET_INTENT_FILTERS);
//如果返回为0 表示没有找到
if(resolveInfos.size()==0){
Log.e("TAG", "没有安装");
}
判断service是否存在
这里service指的是AIDL Service,也就是允许一个Android应用程序访问另外一个应用程序中的类。AIDL Service在调用时需要使用bindService方法进行绑定。如果AIDL Service不存在,则绑定失败,也就是bindService方法会返回false。因此,通过bindService方法就可以判断指定AIDL Service是否存在,
if(!context.bindService(new Intent("com.android.IMyService"),serviceConnection,Context.BIND_AUTO_CREATE)){
Log.e("TAG", "没有服务");
}
判断指定的Content Provider只需要根据ContentResolver对象的相应方法的返回值进行判断即可
Uri uri = Uri.parse("content://mobile.android.regioncontentprovider/cities");
Cursor cursor = context.getContentResolver().query(uri, new String[]{"city_code as_id", "city_name"}, null, null, null);
if(cursor==null){
Log.e("TAG", "没有");
}