下面列出了android.content.pm.PackageManager#queryIntentServices() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean detectDeclaredService(Context cx) {
cx = cx.getApplicationContext();
boolean isDeclared = false;
String packageName = cx.getPackageName();
PackageManager manager = cx.getPackageManager();
String[] surpportActions = {GOOGLE_BIND_INTENT_ACTION, DUWEAR_BIND_INTENT_ACTION, TICWEAR_BIND_INTENT_ACTION};
for (String action : surpportActions) {
Intent intent = new Intent(action);
intent.setPackage(packageName);
List<ResolveInfo> infos = manager.queryIntentServices(intent, PackageManager.GET_INTENT_FILTERS);
if (infos != null && infos.size() > 0) {
isDeclared = true;
break;
}
}
return isDeclared;
}
/**
* Returns the engine info for a given engine name. Note that engines are
* identified by their package name.
*/
public EngineInfo getEngineInfo(String packageName) {
PackageManager pm = mContext.getPackageManager();
Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
intent.setPackage(packageName);
List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent,
PackageManager.MATCH_DEFAULT_ONLY);
// Note that the current API allows only one engine per
// package name. Since the "engine name" is the same as
// the package name.
if (resolveInfos != null && resolveInfos.size() == 1) {
return getEngineInfo(resolveInfos.get(0), pm);
}
return null;
}
/**
* Gets a list of all installed TTS engines.
*
* @return A list of engine info objects. The list can be empty, but never {@code null}.
*/
public List<EngineInfo> getEngines() {
PackageManager pm = mContext.getPackageManager();
Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
List<ResolveInfo> resolveInfos =
pm.queryIntentServices(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfos == null) return Collections.emptyList();
List<EngineInfo> engines = new ArrayList<EngineInfo>(resolveInfos.size());
for (ResolveInfo resolveInfo : resolveInfos) {
EngineInfo engine = getEngineInfo(resolveInfo, pm);
if (engine != null) {
engines.add(engine);
}
}
Collections.sort(engines, EngineInfoComparator.INSTANCE);
return engines;
}
/**
* @return an intent that can launch the settings activity for a given tts engine.
*/
public Intent getSettingsIntent(String engine) {
PackageManager pm = mContext.getPackageManager();
Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
intent.setPackage(engine);
List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent,
PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA);
// Note that the current API allows only one engine per
// package name. Since the "engine name" is the same as
// the package name.
if (resolveInfos != null && resolveInfos.size() == 1) {
ServiceInfo service = resolveInfos.get(0).serviceInfo;
if (service != null) {
final String settings = settingsActivityFromServiceInfo(service, pm);
if (settings != null) {
Intent i = new Intent();
i.setClassName(engine, settings);
return i;
}
}
}
return null;
}
/***
* Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
* "java.lang.IllegalArgumentException: Service Intent must be explicit"
*
* If you are using an implicit intent, and know only 1 target would answer this intent,
* This method will help you turn the implicit intent into the explicit form.
*
* Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466
* @param context
* @param implicitIntent - The original implicit intent
* @return Explicit Intent created from the implicit original intent
*/
public static 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() != 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;
}
/**
* Reloads the list of installed TTS engines.
*
* @param pm The package manager.
* @param results The list to populate with installed TTS engines.
* @return The package for the system default TTS.
*/
public static @Nullable String reloadInstalledTtsEngines(
PackageManager pm, List<String> results) {
final Intent intent = new Intent(TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE);
final List<ResolveInfo> resolveInfos =
pm.queryIntentServices(intent, PackageManager.GET_SERVICES);
String systemTtsEngine = null;
for (ResolveInfo resolveInfo : resolveInfos) {
final ServiceInfo serviceInfo = resolveInfo.serviceInfo;
final ApplicationInfo appInfo = serviceInfo.applicationInfo;
final String packageName = serviceInfo.packageName;
final boolean isSystemApp = ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
results.add(serviceInfo.packageName);
if (isSystemApp) {
systemTtsEngine = packageName;
}
}
return systemTtsEngine;
}
/**
* @return list of currently installed RecognitionService component names flattened to short strings
*/
public List<String> getServices(PackageManager pm) {
List<String> services = new ArrayList<>();
int flags = 0;
//int flags = PackageManager.GET_META_DATA;
List<ResolveInfo> infos = pm.queryIntentServices(
new Intent(RecognitionService.SERVICE_INTERFACE), flags);
for (ResolveInfo ri : infos) {
ServiceInfo si = ri.serviceInfo;
if (si == null) {
Log.i("serviceInfo == null");
continue;
}
String pkg = si.packageName;
String cls = si.name;
// TODO: process si.metaData
String component = (new ComponentName(pkg, cls)).flattenToShortString();
if (!mCombosExcluded.contains(component)) {
services.add(component);
}
}
return services;
}
@Override
protected List<ResolveInfo> loadResolveInfoList() {
PackageManager pm = getActivity().getPackageManager();
Intent trustAgentIntent = new Intent("android.service.trust.TrustAgentService");
List<ResolveInfo> resolveInfos = pm.queryIntentServices(trustAgentIntent,
PackageManager.GET_META_DATA);
List<ResolveInfo> agents = new ArrayList<>();
final int count = resolveInfos.size();
for (int i = 0; i < count; i++) {
ResolveInfo resolveInfo = resolveInfos.get(i);
if (resolveInfo.serviceInfo == null) continue;
agents.add(resolveInfo);
}
return agents;
}
/**
* @return true iff a RecognitionService with the given component name is installed
*/
public static boolean isRecognitionServiceInstalled(PackageManager pm, ComponentName componentName) {
List<ResolveInfo> services = pm.queryIntentServices(
new Intent(RecognitionService.SERVICE_INTERFACE), 0);
for (ResolveInfo ri : services) {
ServiceInfo si = ri.serviceInfo;
if (si == null) {
Log.i("serviceInfo == null");
continue;
}
if (componentName.equals(new ComponentName(si.packageName, si.name))) {
return true;
}
}
return false;
}
/**
* 获取对应action的Service的Intent
*
* @param mContext
* @param serviceAction
* @return
*/
public static Intent getServiceIntentCompatibledApi20(Context mContext, String serviceAction) {
Intent actionIntent = new Intent(serviceAction);
if (Build.VERSION.SDK_INT < 19) {// android 5.0以下
return actionIntent;
}
PackageManager pm = mContext.getPackageManager();
List<ResolveInfo> resultServices = pm.queryIntentServices(actionIntent, 0);
if (resultServices == null) {
return actionIntent;
}
int size = resultServices.size();
if (size == 0) {
return actionIntent;
}
ResolveInfo theFirstService = resultServices.get(0);
String pkgName = theFirstService.serviceInfo.packageName;
String className = theFirstService.serviceInfo.name;
ComponentName componentName = new ComponentName(pkgName, className);
actionIntent.setComponent(componentName);
return actionIntent;
}
/***
* Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
* "java.lang.IllegalArgumentException: Service Intent must be explicit"
* If you are using an implicit intent, and know only 1 target would answer this intent,
* This method will help you turn the implicit intent into the explicit form.
* Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466
*
* @param context
* the context
* @param implicitIntent
* - The original implicit intent
* @return Explicit Intent created from the implicit original intent
*/
public static Intent implicit2ExplicitIntent(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;
}
/**
* Returns the engine info for a given engine name. Note that engines are
* identified by their package name.
*/
public static TtsEngineInfo getEngineInfo(Context context, String packageName) {
if (packageName == null) {
return null;
}
final PackageManager pm = context.getPackageManager();
final Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE).setPackage(packageName);
final List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if ((resolveInfos == null) || resolveInfos.isEmpty()) {
return null;
}
// Note that the current API allows only one engine per
// package name. Since the "engine name" is the same as
// the package name.
return getEngineInfo(resolveInfos.get(0), pm);
}
private void validateService(String serviceName) {
if (serviceName == null) throw new NullPointerException("No service provided");
Intent taskIntent = new Intent(ACTION_TASK_READY);
taskIntent.setPackage(context.getPackageName());
PackageManager pm = context.getPackageManager();
List<ResolveInfo> serviceResolves = pm.queryIntentServices(taskIntent, 0);
if (serviceResolves == null || serviceResolves.isEmpty())
throw new IllegalArgumentException("No service found");
for (ResolveInfo info : serviceResolves) {
if (serviceName.equals(info.serviceInfo.name)) return;
}
throw new IllegalArgumentException("Service not supported.");
}
static boolean isOpenKeychainInstalled(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String provider = prefs.getString("openpgp_provider", "org.sufficientlysecure.keychain");
PackageManager pm = context.getPackageManager();
Intent intent = new Intent(OpenPgpApi.SERVICE_INTENT_2);
intent.setPackage(provider);
List<ResolveInfo> ris = pm.queryIntentServices(intent, 0);
return (ris.size() > 0);
}
@Override
public boolean initialize(Context context, InAppBillingV3Vendor vendor, LifecycleListener listener,
Logger logger) {
final boolean superInited = super.initialize(context, vendor, listener, logger);
this.listener = listener;
if (available()) {
if (listener != null) {
listener.initialized(true);
}
return true;
}
final Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage(VENDOR_PACKAGE);
final PackageManager packageManager = context.getPackageManager();
if (packageManager == null) {
return false;
} else {
final List<ResolveInfo> intentServices
= packageManager.queryIntentServices(serviceIntent, 0);
if (intentServices == null || intentServices.isEmpty()) {
return false;
}
}
try {
return superInited
&& context.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} catch (NullPointerException e) {
// Some incompatible devices will throw a NPE here with the message:
// Attempt to read from field 'int com.android.server.am.ProcessRecord.uid' on a null object reference
// while attempting to unparcel some information within ActivityManagerNative.
// There is not much we can do about this, so we're going to default to returning false
// since we are unable to bind the service, which means the vendor is not available.
return false;
}
}
private void doBindService() {
Connection localConnection = new Connection();
Intent serviceIntent = new Intent(ACTION_DISPLAY_SERVICE);
PackageManager pm = mContext.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(serviceIntent, 0);
if (resolveInfo == null || resolveInfo.isEmpty()) {
Log.e(LOG_TAG, "Unable to create serviceIntent");
return;
}
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(serviceIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
if (!mContext.bindService(explicitIntent, localConnection,
Context.BIND_AUTO_CREATE)) {
Log.e(LOG_TAG, "Failed to bind Service");
mHandler.scheduleRebind();
return;
}
mConnection = localConnection;
Log.i(LOG_TAG, "Bound to braille service");
}
@Nullable
public static Intent getOpenStoreIntent(@NonNull final Context context) {
final Intent intent = new Intent(ACTION_BIND_OPENSTORE);
final PackageManager packageManager = context.getPackageManager();
final List<ResolveInfo> resolveInfos = packageManager.queryIntentServices(intent, 0);
if (resolveInfos != null && !resolveInfos.isEmpty()) {
final Intent explicitIntent = new Intent(intent);
final ServiceInfo serviceInfo = resolveInfos.get(0).serviceInfo;
explicitIntent.setClassName(serviceInfo.packageName, serviceInfo.name);
return explicitIntent;
}
return null;
}
public static boolean isSinaWeiboSafe(Activity activity) {
Intent mapCall = new Intent("com.sina.weibo.remotessoservice");
PackageManager packageManager = activity.getPackageManager();
List<ResolveInfo> services = packageManager.queryIntentServices(mapCall, 0);
return services.size() > 0;
}
public static boolean isSinaWeiboSafe(Activity activity) {
Intent mapCall = new Intent("com.sina.weibo.remotessoservice");
PackageManager packageManager = activity.getPackageManager();
List<ResolveInfo> services = packageManager.queryIntentServices(mapCall, 0);
return services.size() > 0;
}
/**
* Indicates whether the specified action can be used as an intent. This
* method queries the package manager for installed packages that can
* respond to an intent with the specified action. If no suitable package is
* found, this method returns false.
*
* @param context The application's environment.
* @param action The Intent action to check for availability.
* @return True if an Intent with the specified action can be sent and
* responded to, false otherwise.
*/
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list = packageManager.queryIntentServices(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}