Example usage for android.content.pm PackageManager queryIntentServices

List of usage examples for android.content.pm PackageManager queryIntentServices

Introduction

In this page you can find the example usage for android.content.pm PackageManager queryIntentServices.

Prototype

public abstract List<ResolveInfo> queryIntentServices(Intent intent, @ResolveInfoFlags int flags);

Source Link

Document

Retrieve all services that can match the given intent.

Usage

From source file:Main.java

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;
    }/*from ww w  .  j  a  v  a2 s . c  om*/

    //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;
}

From source file:Main.java

public static boolean isServiceAvailable(Context context, Class<?> service) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(context, service);
    List resolveInfo = packageManager.queryIntentServices(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (resolveInfo.size() > 0) {
        return true;
    }/*  www .j  a  v  a2 s  .  c o m*/
    return false;
}

From source file:cz.maresmar.sfm.service.plugin.PluginUtils.java

/**
 * Starts plugin according to API level// ww  w. j  ava  2 s.  com
 *
 * @param context Some valid context
 * @param intent  Intent of plugin to be started
 * @see JobIntentService#enqueueWork(Context, ComponentName, int, Intent)
 */
public static void startPlugin(@NonNull Context context, @NonNull Intent intent) {
    // Test if the plugin exists
    PackageManager manager = context.getPackageManager();
    if (manager.queryIntentServices(intent, 0).size() != 1) {
        Timber.e("Plugin %s not found", intent.getComponent());
        throw new IllegalArgumentException("Plugin not found " + intent.getComponent());
    }

    // Finds jobId for selected plugin
    String pluginName = ProviderContract.buildString(intent.getComponent());
    int jobId;
    if (!mPluginsIds.containsKey(pluginName)) {
        jobId = mPluginsIds.size();
        mPluginsIds.put(pluginName, jobId);
    } else {
        jobId = mPluginsIds.get(pluginName);
    }

    // Starts plugin according to API level
    if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            || (context.getPackageName().equals(intent.getComponent().getPackageName()))) {
        JobIntentService.enqueueWork(context, intent.getComponent(), jobId, intent);
    } else {
        // On Android >= O with external plugin use BroadcastContract.BROADCAST_PLAN_RUN to
        // start plugin, because planning of external APK's service is not allowed

        Intent plan = new Intent();
        // Explicitly select a package to communicate with
        plan.setPackage(intent.getComponent().getPackageName());
        plan.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);

        plan.setAction(ActionContract.BROADCAST_PLAN_RUN);
        plan.putExtra(ActionContract.EXTRA_JOB_ID, jobId);
        plan.putExtra(ActionContract.EXTRA_INTENT_TO_DO, intent);
        context.sendBroadcast(plan);
    }
}

From source file:jahirfiquitiva.iconshowcase.utilities.utils.NotificationUtils.java

public static boolean isServiceAvailable(Context context, Class service) {
    if (context == null)
        return false;
    try {//from w  ww  . j  a v  a 2s  . c  om
        final PackageManager packageManager = context.getPackageManager();
        final Intent intent = new Intent(context, service);
        List resolveInfo = packageManager.queryIntentServices(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return resolveInfo.size() > 0;
    } catch (Exception ex) {
        return false;
    }
}

From source file:com.mobilesolutionworks.android.httpcache.HttpCacheLoaderImpl.java

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;
    }/*w ww  . ja  va  2s .  c  om*/

    // 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;
}

From source file:com.onesignal.NotificationExtenderService.java

static Intent getIntent(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent intent = new Intent().setAction("com.onesignal.NotificationExtender")
            .setPackage(context.getPackageName());
    List<ResolveInfo> resolveInfo = packageManager.queryIntentServices(intent, PackageManager.GET_META_DATA);
    if (resolveInfo.size() < 1)
        return null;

    return intent;
}

From source file:androidx.media.session.MediaButtonReceiver.java

private static ComponentName getServiceComponentByAction(Context context, String action) {
    PackageManager pm = context.getPackageManager();
    Intent queryIntent = new Intent(action);
    queryIntent.setPackage(context.getPackageName());
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, 0 /* flags */);
    if (resolveInfos.size() == 1) {
        ResolveInfo resolveInfo = resolveInfos.get(0);
        return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
    } else if (resolveInfos.isEmpty()) {
        return null;
    } else {/*w  w  w . jav  a 2 s.  c  o m*/
        throw new IllegalStateException(
                "Expected 1 service that handles " + action + ", found " + resolveInfos.size());
    }
}

From source file:ir.aarani.bazaar.billing.BillingProcessor.java

public static boolean isIabServiceAvailable(Context context) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    List<ResolveInfo> list = packageManager.queryIntentServices(intent, 0);
    return list.size() > 0;
}

From source file:com.blanyal.remindme.MainActivity.java

public static Intent convertImplicitIntentToExplicitIntent(Intent implicitIntent, Context context) {
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> resolveInfoList = pm.queryIntentServices(implicitIntent, 0);

    if (resolveInfoList == null || resolveInfoList.size() != 1) {
        return null;
    }/*from   w w w.j  av  a  2  s.co  m*/
    ResolveInfo serviceInfo = resolveInfoList.get(0);

    Log.v("vbharill", "package name" + serviceInfo.serviceInfo.packageName);
    Log.v("vbharill", "service name" + serviceInfo.serviceInfo.name);
    ComponentName component = new ComponentName(serviceInfo.serviceInfo.packageName,
            serviceInfo.serviceInfo.name);
    Intent explicitIntent = new Intent(implicitIntent);
    explicitIntent.setComponent(component);
    return explicitIntent;
}

From source file:com.cloudzilla.fb.FacebookServiceProxy.java

/**
 * Determine if this is running on the 1App platform.
 *
 * @param context the app context/*from   www  .  ja v a2  s  . c o m*/
 * @return true if running on the 1App platform
 */
public static boolean isOn1App(Context context) {
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> list = pm.queryIntentServices(FACEBOOK_SERVICE_INTENT, 0);
    return list.size() > 0;
}