List of usage examples for android.content.pm PackageManager queryBroadcastReceivers
public abstract List<ResolveInfo> queryBroadcastReceivers(Intent intent, @ResolveInfoFlags int flags);
From source file:Main.java
public static boolean canResolveBroadcast(Context context, Intent intent) { PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0); return receivers != null && receivers.size() > 0; }
From source file:Main.java
static Pair<String, Resources> findSystemApk(String action, PackageManager pm) { final Intent intent = new Intent(action); for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) { if (info.activityInfo != null && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { final String packageName = info.activityInfo.packageName; try { final Resources res = pm.getResourcesForApplication(packageName); return Pair.create(packageName, res); } catch (NameNotFoundException e) { Log.w(TAG, "Failed to find resources for " + packageName); }// w w w .j a va 2s . c o m } } return null; }
From source file:Main.java
/** * Utility method to check if a shortcut can be added to the homescreen. * @param context Context used to get the package manager. * @return if a shortcut can be added to the homescreen under the current profile. *///w w w .ja va2s . co m public static boolean isAddToHomeIntentSupported(Context context) { PackageManager pm = context.getPackageManager(); Intent i = new Intent(INSTALL_SHORTCUT); List<ResolveInfo> receivers = pm.queryBroadcastReceivers(i, PackageManager.GET_INTENT_FILTERS); return !receivers.isEmpty(); }
From source file:Main.java
/** * Utility method to check if a shortcut can be added to the home screen. * @param context Context used to get the package manager. * @return if a shortcut can be added to the home screen under the current profile. *///from w w w .j ava 2s. co m // TODO(crbug.com/635567): Fix this properly. @SuppressLint("WrongConstant") public static boolean isAddToHomeIntentSupported(Context context) { PackageManager pm = context.getPackageManager(); Intent i = new Intent(INSTALL_SHORTCUT); List<ResolveInfo> receivers = pm.queryBroadcastReceivers(i, PackageManager.GET_INTENT_FILTERS); return !receivers.isEmpty(); }
From source file:com.commonsware.android.broadcast.fanout.MainActivity.java
private static void sendImplicitBroadcast(Context ctxt, Intent i) { PackageManager pm = ctxt.getPackageManager(); List<ResolveInfo> matches = pm.queryBroadcastReceivers(i, 0); for (ResolveInfo resolveInfo : matches) { Intent explicit = new Intent(i); ComponentName cn = new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName, resolveInfo.activityInfo.name); explicit.setComponent(cn);/*from w w w. j a v a2 s .co m*/ ctxt.sendBroadcast(explicit); } }
From source file:Main.java
@NonNull private static String prepareCheckReceiverReport(@NonNull final Context context, @Nullable final String exceptionMessage, @Nullable final String receiverName, @NonNull final Intent broadcastIntent) { final StringBuilder reportBuilder = new StringBuilder("checkReceiver error").append(LINE_SEPARATOR) .append("Exception message : ").append(exceptionMessage).append(LINE_SEPARATOR) .append("Expected receiverName : ").append(receiverName).append(LINE_SEPARATOR) .append("Intent action : ").append(broadcastIntent.getAction()); final PackageManager packageManager = context.getPackageManager(); final List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(broadcastIntent, PackageManager.GET_INTENT_FILTERS); if (receivers == null) { reportBuilder.append(LINE_SEPARATOR).append("queryBroadcastReceivers returns null"); } else if (receivers.isEmpty()) { reportBuilder.append(LINE_SEPARATOR).append("queryBroadcastReceivers returns empty list"); } else {//from w w w.j av a 2s .co m reportBuilder.append(LINE_SEPARATOR) .append("queryBroadcastReceivers returns not empty list. List size : ").append(receivers.size()) .append(LINE_SEPARATOR).append("PackageName : ").append(context.getPackageName()); int emptyPackageNameCounter = 0; int emptyReceiverNameCounter = 0; for (ResolveInfo receiver : receivers) { if (TextUtils.isEmpty(receiver.activityInfo.packageName)) { ++emptyPackageNameCounter; } else if (receiver.activityInfo.packageName.equals(context.getPackageName())) { reportBuilder.append(LINE_SEPARATOR).append("Receiver with right package : ") .append(receiver.activityInfo.name); } if (TextUtils.isEmpty(receiver.activityInfo.name)) { ++emptyReceiverNameCounter; } else if (receiver.activityInfo.name.equals(receiverName)) { reportBuilder.append(LINE_SEPARATOR).append("Receiver with right name has package : ") .append(receiver.activityInfo.packageName); } } reportBuilder.append(LINE_SEPARATOR).append("Receivers with empty package name : ") .append(emptyPackageNameCounter).append(LINE_SEPARATOR).append("Receivers with empty name : ") .append(emptyReceiverNameCounter); } return reportBuilder.toString(); }
From source file:Main.java
static List<Pair<String, Resources>> findSystemApks(String action, PackageManager pm) { final Intent intent = new Intent(action); List<Pair<String, Resources>> systemApks = new ArrayList<Pair<String, Resources>>(); for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) { if (info.activityInfo != null && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { final String packageName = info.activityInfo.packageName; try { final Resources res = pm.getResourcesForApplication(packageName); systemApks.add(Pair.create(packageName, res)); } catch (NameNotFoundException e) { Log.w(TAG, "Failed to find resources for " + packageName); }// w w w. j a v a 2s .co m } } return systemApks; }
From source file:util.mediamanager.PlaylistUtils.java
public static void ListMediaButtonReceivers(Context context, String query) { try {/*from www .jav a2 s . co m*/ final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> mediaReceivers = packageManager.queryBroadcastReceivers(mediaButtonIntent, PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RESOLVED_FILTER); for (int i = mediaReceivers.size() - 1; i >= 0; i--) { ResolveInfo mediaReceiverResolveInfo = mediaReceivers.get(i); String name = mediaReceiverResolveInfo.activityInfo.applicationInfo.sourceDir + ", " + mediaReceiverResolveInfo.activityInfo.name; String cn = mediaReceiverResolveInfo.resolvePackageName; String cn1 = mediaReceiverResolveInfo.activityInfo.toString(); Log.d(Constants.APP.TAG, "resolvePackageName media receivers = " + cn); Log.d(Constants.APP.TAG, "activityInfo = " + cn1); } mediaButtonIntent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH); //mediaButtonIntent.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.MainActivity")); mediaButtonIntent.putExtra(SearchManager.QUERY, "GM1"); mediaButtonIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(mediaButtonIntent); } catch (ActivityNotFoundException e) { Log.e(Constants.APP.TAG, "Error searching Spotify w/ query '" + query + "'"); //Toast.makeText(mRideManager.mAppContext, String.format("Error parsing query \"%s\"", query), Toast.LENGTH_SHORT).show(); e.printStackTrace(); } }
From source file:androidx.media.session.MediaButtonReceiver.java
/** * @hide//from w ww .j av a2s . co m */ @RestrictTo(LIBRARY) public static ComponentName getMediaButtonReceiverComponent(Context context) { Intent queryIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); queryIntent.setPackage(context.getPackageName()); PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0); if (resolveInfos.size() == 1) { ResolveInfo resolveInfo = resolveInfos.get(0); return new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name); } else if (resolveInfos.size() > 1) { Log.w(TAG, "More than one BroadcastReceiver that handles " + Intent.ACTION_MEDIA_BUTTON + " was found, returning null."); } return null; }
From source file:com.google.android.gcm.GCMRegistrar.java
private static void checkReceiver(Context context, Set<String> allowedReceivers, String action) { PackageManager pm = context.getPackageManager(); String packageName = context.getPackageName(); Intent intent = new Intent(action); intent.setPackage(packageName);/*from ww w .j a va 2s .c om*/ List<ResolveInfo> receivers = pm.queryBroadcastReceivers(intent, PackageManager.GET_INTENT_FILTERS); if (receivers.isEmpty()) { throw new IllegalStateException("No receivers for action " + action); } if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Found " + receivers.size() + " receivers for action " + action); } // make sure receivers match for (ResolveInfo receiver : receivers) { String name = receiver.activityInfo.name; if (!allowedReceivers.contains(name)) { throw new IllegalStateException( "Receiver " + name + " is not set with permission " + GCMConstants.PERMISSION_GCM_INTENTS); } } }