Java Code Examples for android.content.pm.PackageManager.queryIntentServices()

The following are Jave code examples for showing how to use queryIntentServices() of the android.content.pm.PackageManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Phoenix-for-VK   File: Utils.java   View Source Code Vote up 7 votes
/***
 * Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
 * "java.lang.IllegalArgumentException: Service Intent must be explicit"
 * <p>
 * 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.
 * <p>
 * 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;
}
 
Example 2
Project: AI-Powered-Intelligent-Banking-Platform   File: RecognitionServiceManager.java   View Source Code Vote up 7 votes
/**
 * @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;
}
 
Example 3
Project: letv   File: MediaButtonReceiver.java   View Source Code Vote up 6 votes
public void onReceive(Context context, Intent intent) {
    Intent queryIntent = new Intent("android.intent.action.MEDIA_BUTTON");
    queryIntent.setPackage(context.getPackageName());
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, 0);
    if (resolveInfos.isEmpty()) {
        queryIntent.setAction(MediaBrowserServiceCompat.SERVICE_INTERFACE);
        resolveInfos = pm.queryIntentServices(queryIntent, 0);
    }
    if (resolveInfos.isEmpty()) {
        throw new IllegalStateException("Could not find any Service that handles android.intent.action.MEDIA_BUTTON or a media browser service implementation");
    } else if (resolveInfos.size() != 1) {
        throw new IllegalStateException("Expected 1 Service that handles " + queryIntent.getAction() + ", found " + resolveInfos.size());
    } else {
        ResolveInfo resolveInfo = (ResolveInfo) resolveInfos.get(0);
        intent.setComponent(new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name));
        context.startService(intent);
    }
}
 
Example 4
Project: letv   File: a.java   View Source Code Vote up 6 votes
public static List<ComponentName> t(Context context) {
    z.b();
    List<ComponentName> arrayList = new ArrayList();
    PackageManager packageManager = context.getPackageManager();
    Intent intent = new Intent();
    intent.setAction(z[98]);
    List queryIntentServices = packageManager.queryIntentServices(intent, 0);
    if (queryIntentServices == null || queryIntentServices.size() == 0) {
        return null;
    }
    for (int i = 0; i < queryIntentServices.size(); i++) {
        ServiceInfo serviceInfo = ((ResolveInfo) queryIntentServices.get(i)).serviceInfo;
        String str = serviceInfo.name;
        String str2 = serviceInfo.packageName;
        if (serviceInfo.exported && serviceInfo.enabled && !e.c.equals(str2)) {
            new StringBuilder(z[97]).append(str2).append("/").append(str).append("}");
            z.b();
            arrayList.add(new ComponentName(str2, str));
        }
    }
    return arrayList;
}
 
Example 5
Project: boohee_v5.6   File: MediaButtonReceiver.java   View Source Code Vote up 6 votes
public void onReceive(Context context, Intent intent) {
    Intent queryIntent = new Intent("android.intent.action.MEDIA_BUTTON");
    queryIntent.setPackage(context.getPackageName());
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, 0);
    if (resolveInfos.isEmpty()) {
        queryIntent.setAction(MediaBrowserServiceCompat.SERVICE_INTERFACE);
        resolveInfos = pm.queryIntentServices(queryIntent, 0);
    }
    if (resolveInfos.isEmpty()) {
        throw new IllegalStateException("Could not find any Service that handles android.intent.action.MEDIA_BUTTON or a media browser service implementation");
    } else if (resolveInfos.size() != 1) {
        throw new IllegalStateException("Expected 1 Service that handles " + queryIntent.getAction() + ", found " + resolveInfos.size());
    } else {
        ResolveInfo resolveInfo = (ResolveInfo) resolveInfos.get(0);
        intent.setComponent(new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name));
        context.startService(intent);
    }
}
 
Example 6
Project: AI-Powered-Intelligent-Banking-Platform   File: RecognitionServiceManager.java   View Source Code Vote up 6 votes
/**
 * @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;
}
 
Example 7
Project: AI-Powered-Intelligent-Banking-Platform   File: RecognitionServiceManager.java   View Source Code Vote up 6 votes
/**
 * @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;
}
 
Example 8
Project: AI-Powered-Intelligent-Banking-Platform   File: RecognitionServiceManager.java   View Source Code Vote up 6 votes
/**
 * @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;
}
 
Example 9
Project: android-media-controller   File: MediaAppControllerActivity.java   View Source Code Vote up 5 votes
private void connectToMediaBrowserPackage(final String packageName) {
    final PackageManager packageManager = getPackageManager();

    final Intent mediaBrowserIntent = new Intent(MediaBrowserServiceCompat.SERVICE_INTERFACE);
    final List<ResolveInfo> services =
            packageManager.queryIntentServices(mediaBrowserIntent,
                    PackageManager.GET_RESOLVED_FILTER);
    for (ResolveInfo info : services) {
        if (info.serviceInfo.packageName.equals(packageName)) {
            final Bitmap icon = BitmapUtils.convertDrawable(
                    getResources(), info.loadIcon(packageManager));
            final String name = info.loadLabel(packageManager).toString();

            setupToolbar(name, icon);
            MediaAppEntry appEntry = MediaAppEntry.fromBrowseService(
                    info.serviceInfo, packageManager);
            appEntry.getSessionToken(this, new MediaAppEntry.SessionTokenAvailableCallback() {
                @Override
                public void onSuccess(MediaSessionCompat.Token sessionToken) {
                    mMediaAppDetails = new MediaAppDetails(name, icon, sessionToken);
                    setupMediaController();
                }

                @Override
                public void onFailure() {
                    showToastAndFinish(getString(R.string.connection_failed_msg, packageName));
                }
            });
            return;
        }
    }
    // Failed to find package.
    showToastAndFinish(getString(R.string.no_app_for_package, packageName));
}
 
Example 10
Project: atlas   File: ServiceDetector.java   View Source Code Vote up 5 votes
/***
 * 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 implicitIntent;
    }

    // Get component info and create ComponentName
    ResolveInfo serviceInfo = resolveInfo.get(0);
    String packageName = serviceInfo.serviceInfo.packageName;
    String className = serviceInfo.serviceInfo.name;
    if(packageName!=null && packageName.equals(context.getPackageName())) {
        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;
    }else{
        return implicitIntent;
    }
}