下面列出了android.content.pm.PackageManager#queryIntentActivities() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Used to check whether there is a specialized handler for a given intent.
* @param intent The intent to check with.
* @return Whether there is a specialized handler for the given intent.
*/
private static boolean hasSpecializedHandlerIntents(Context context, Intent intent) {
try {
PackageManager pm = context.getPackageManager();
List<ResolveInfo> handlers = pm.queryIntentActivities(
intent,
PackageManager.GET_RESOLVED_FILTER);
if (handlers == null || handlers.size() == 0) {
return false;
}
for (ResolveInfo resolveInfo : handlers) {
IntentFilter filter = resolveInfo.filter;
if (filter == null) continue;
if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) continue;
if (resolveInfo.activityInfo == null) continue;
return true;
}
} catch (RuntimeException e) {
Log.e(TAG, "Runtime exception while getting specialized handlers");
}
return false;
}
public Map<String, String> getIconPacks() {
Map<String, String> iconPacks = new HashMap<String, String>();
PackageManager pm = context.getPackageManager();
List<ResolveInfo> iconPacksInfo = pm.queryIntentActivities(new Intent("org.adw.launcher.THEMES"), PackageManager.GET_META_DATA);
String iconPackPackage = null;
String iconPackName = null;
for (ResolveInfo info: iconPacksInfo) {
iconPackPackage = info.activityInfo.packageName;
ApplicationInfo ai = null;
try {
ai = pm.getApplicationInfo(iconPackPackage, PackageManager.GET_META_DATA);
iconPackName = pm.getApplicationLabel(ai).toString();
} catch (PackageManager.NameNotFoundException e) {}
iconPacks.put(iconPackName, iconPackPackage);
}
return iconPacks;
}
/**
* Used to check whether there is a specialized handler for a given intent.
* @param intent The intent to check with.
* @return Whether there is a specialized handler for the given intent.
*/
private static boolean hasSpecializedHandlerIntents(Context context, Intent intent) {
try {
PackageManager pm = context.getPackageManager();
List<ResolveInfo> handlers = pm.queryIntentActivities(
intent,
PackageManager.GET_RESOLVED_FILTER);
if (handlers == null || handlers.size() == 0) {
return false;
}
for (ResolveInfo resolveInfo : handlers) {
IntentFilter filter = resolveInfo.filter;
if (filter == null) continue;
if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) continue;
if (resolveInfo.activityInfo == null) continue;
return true;
}
} catch (RuntimeException e) {
Log.e(TAG, "Runtime exception while getting specialized handlers");
}
return false;
}
private static String getLauncherClassName(Context context) {
PackageManager pm = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfos) {
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
if (pkgName.equalsIgnoreCase(context.getPackageName())) {
String className = resolveInfo.activityInfo.name;
return className;
}
}
return null;
}
public boolean isPackageExist(String name) {
boolean isExist = false;
PackageManager pkgMgr = getPackageManager();
List<ResolveInfo> mApps;
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = pkgMgr.queryIntentActivities(mainIntent, 0);
try {
for (int i = 0; i < mApps.size(); i++) {
if (mApps.get(i).activityInfo.packageName.startsWith(name)) {
isExist = true;
break;
}
}
} catch (Exception e) {
isExist = false;
}
return isExist;
}
protected List<SampleInfo> querySampleActivities() {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setPackage(getPackageName());
intent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
PackageManager pm = getPackageManager();
List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
ArrayList<SampleInfo> samples = new ArrayList<SampleInfo>();
final int count = infos.size();
for (int i = 0; i < count; i++) {
final ResolveInfo info = infos.get(i);
final CharSequence labelSeq = info.loadLabel(pm);
String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name;
Intent target = new Intent();
target.setClassName(info.activityInfo.applicationInfo.packageName,
info.activityInfo.name);
SampleInfo sample = new SampleInfo(label, target);
samples.add(sample);
}
return samples;
}
private String findTargetAppPackage(Intent intent) {
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> availableApps = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (availableApps != null) {
for (ResolveInfo availableApp : availableApps) {
String packageName = availableApp.activityInfo.packageName;
if (targetApplications.contains(packageName)) {
return packageName;
}
}
}
return null;
}
/**
* Search for intent handlers that are specific to this URL aka, specialized
* apps like google maps or youtube
*/
private boolean isSpecializedHandlerAvailable(@NonNull Intent intent) {
PackageManager pm = mActivity.getPackageManager();
List<ResolveInfo> handlers = pm.queryIntentActivities(intent,
PackageManager.GET_RESOLVED_FILTER);
if (handlers == null || handlers.isEmpty()) {
return false;
}
for (ResolveInfo resolveInfo : handlers) {
IntentFilter filter = resolveInfo.filter;
if (filter == null) {
// No intent filter matches this intent?
// Error on the side of staying in the browser, ignore
continue;
}
// NOTICE: Use of && instead of || will cause the browser
// to launch a new intent for every URL, using OR only
// launches a new one if there is a non-browser app that
// can handle it.
// Previously we checked the number of data paths, but it is unnecessary
// filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0
if (filter.countDataAuthorities() == 0) {
// Generic handler, skip
continue;
}
return true;
}
return false;
}
private boolean canHandleIntent(Intent intent) {
Activity activity = getActivity();
if (activity == null) {
return false;
}
PackageManager manager = activity.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
return (infos != null) && !infos.isEmpty();
}
/**
* Checks whether the speech recognition is available on the device or not.
*
* @param context The context
*
* @return true if available; false otherwise
*/
public static boolean isSpeechRecognitionAvailable(@NonNull Context context) {
Preconditions.nonNull(context);
final PackageManager packageManager = context.getPackageManager();
final List<ResolveInfo> activities = packageManager.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
PackageManager.MATCH_DEFAULT_ONLY
);
return ((activities != null) && (activities.size() > 0));
}
/** Launch a SMS intent if the device is capable.
*
* @param activity The parent activity (for context)
* @param number The number to sms (not the full URI)
* @param text The sms body
*/
public static void launchSmsIntent(final Activity activity, String number, String text) {
Log.i(LOG_TAG,"Launch SMS intent to "+number);
// create sms intent
Uri smsUri = Uri.parse("smsto:" + number);
Intent smsIntent = new Intent(Intent.ACTION_SENDTO, smsUri);
smsIntent.putExtra("sms_body", text);
// make sure there is an activity which can handle the intent.
PackageManager smspackageManager = activity.getPackageManager();
List<ResolveInfo> smsresolveInfos = smspackageManager.queryIntentActivities(smsIntent, 0);
if(smsresolveInfos.size() > 0) {
activity.startActivity(smsIntent);
}
}
/**
* Search for intent handlers that are specific to this URL aka, specialized
* apps like google maps or youtube
*/
private boolean isSpecializedHandlerAvailable(@NonNull Intent intent) {
PackageManager pm = mActivity.getPackageManager();
List<ResolveInfo> handlers = pm.queryIntentActivities(intent,
PackageManager.GET_RESOLVED_FILTER);
if (handlers == null || handlers.isEmpty()) {
return false;
}
for (ResolveInfo resolveInfo : handlers) {
IntentFilter filter = resolveInfo.filter;
if (filter == null) {
// No intent filter matches this intent?
// Error on the side of staying in the browser, ignore
continue;
}
// NOTICE: Use of && instead of || will cause the browser
// to launch a new intent for every URL, using OR only
// launches a new one if there is a non-browser app that
// can handle it.
// Previously we checked the number of data paths, but it is unnecessary
// filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0
if (filter.countDataAuthorities() == 0) {
// Generic handler, skip
continue;
}
return true;
}
return false;
}
private void getAppList() {
PackageManager manager = requireActivity().getPackageManager();
List<String> entries = new ArrayList<>();
List<String> entryValues = new ArrayList<>();
// Get default value.
entries.add(getString(R.string.gesture_action_default));
entryValues.add(getString(R.string.gesture_action_default_value));
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = manager.queryIntentActivities(intent, 0);
Collections.sort(availableActivities, new ResolveInfo.DisplayNameComparator(manager));
// Fetch apps and feed it into our list.
for (ResolveInfo resolveInfo : availableActivities) {
String appName = resolveInfo.loadLabel(manager).toString();
String packageName = resolveInfo.activityInfo.packageName + "/" + resolveInfo.activityInfo.name;
entries.add(appName);
entryValues.add(packageName);
}
appListEntries = entries.toArray(new CharSequence[0]);
appListEntryValues = entryValues.toArray(new CharSequence[0]);
}
public Intent createPhotoShareIntent(
Context context,
AppAccount appAccount,
String experimentId,
String imageName,
String imageCaption) {
if (!ExportService.canShare(context, appAccount)) {
return null;
}
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
File imageFile = new File(getExperimentDirectory(appAccount, experimentId), imageName);
Uri uri = FileProvider.getUriForFile(context, context.getPackageName(), imageFile);
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, imageCaption);
PackageManager packageManager = context.getPackageManager();
List activities =
packageManager.queryIntentActivities(shareIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (activities.size() > 0) {
return shareIntent;
}
return null;
}
private static boolean couldOpen(PackageManager pm, Context context, Intent resolveIntent) {
List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
if (apps == null || apps.size() < 1) {
return false;
}
Iterator<ResolveInfo> it = apps.iterator();
if (it.hasNext()) {
return true;
} else {
return false;
}
}
public static boolean isGooglePlaySafe(Activity activity) {
Uri uri = Uri.parse("http://play.google.com/store/apps/details?id=com.google.android.gms");
Intent mapCall = new Intent(Intent.ACTION_VIEW, uri);
mapCall.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
mapCall.setPackage("com.android.vending");
PackageManager packageManager = activity.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mapCall, 0);
return activities.size() > 0;
}
/**
* Chooses an appropriate provider (see class description) and the launch mode that browser
* supports.
*/
public static Action pickProvider(PackageManager pm) {
// TODO(peconn): Should we use "https://" instead?
Intent queryBrowsersIntent = new Intent()
.setAction(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_BROWSABLE)
.setData(Uri.parse("http://"));
if (sPackageNameForTesting != null) {
queryBrowsersIntent.setPackage(sPackageNameForTesting);
}
String bestCctProvider = null;
String bestBrowserProvider = null;
// These packages will be in order of Android's preference.
List<ResolveInfo> possibleProviders
= pm.queryIntentActivities(queryBrowsersIntent, PackageManager.MATCH_DEFAULT_ONLY);
// According to the documentation, the flag we want to use above is MATCH_DEFAULT_ONLY.
// This would match all the browsers installed on the user's system whose intent handler
// contains the category Intent.CATEGORY_DEFAULT. However, in Android M the behavior of
// the PackageManager changed to only return the default browser unless the MATCH_ALL is
// passed (this is specific to querying browsers - if you query for any other type of
// package, MATCH_DEFAULT_ONLY will work as documented). This flag did not exist on Android
// versions before M, so we only use it in that case.
//
// Additionally we add the result of the call with MATCH_ALL onto the end of the result of
// MATCH_DEFAULT_ONLY (instead of calling queryIntentActivities just once, with MATCH_ALL)
// because (again, as opposed to the documentation) when MATCH_ALL is used the results are
// not returned in order of Android's preference.
//
// This will result in the user's default browser being in the list twice, however that
// shouldn't affect the correctness of the following code.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
possibleProviders.addAll(pm.queryIntentActivities(queryBrowsersIntent,
PackageManager.MATCH_ALL));
}
Map<String, Integer> customTabsServices = getLaunchModesForCustomTabsServices(pm);
for (ResolveInfo possibleProvider : possibleProviders) {
String providerName = possibleProvider.activityInfo.packageName;
@LaunchMode int launchMode = customTabsServices.containsKey(providerName)
? customTabsServices.get(providerName) : LaunchMode.BROWSER;
switch (launchMode) {
case LaunchMode.TRUSTED_WEB_ACTIVITY:
Log.d(TAG, "Found TWA provider, finishing search: " + providerName);
return new Action(LaunchMode.TRUSTED_WEB_ACTIVITY, providerName);
case LaunchMode.CUSTOM_TAB:
Log.d(TAG, "Found Custom Tabs provider: " + providerName);
if (bestCctProvider == null) bestCctProvider = providerName;
break;
case LaunchMode.BROWSER:
Log.d(TAG, "Found browser: " + providerName);
if (bestBrowserProvider == null) bestBrowserProvider = providerName;
break;
}
}
if (bestCctProvider != null) {
Log.d(TAG, "Found no TWA providers, using first Custom Tabs provider: "
+ bestCctProvider);
return new Action(LaunchMode.CUSTOM_TAB, bestCctProvider);
}
Log.d(TAG, "Found no TWA providers, using first browser: " + bestBrowserProvider);
return new Action(LaunchMode.BROWSER, bestBrowserProvider);
}
protected List<Map<String, Object>> getData(String prefix) {
List<Map<String, Object>> myData = new ArrayList<>();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(CATEGORY_ACTIVITIES);
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
if (null == list) {
return myData;
}
String[] prefixPath;
String prefixWithSlash = prefix;
if (prefix.equals("")) {
prefixPath = null;
} else {
prefixPath = prefix.split("/");
prefixWithSlash = prefix + "/";
}
int len = list.size();
Map<String, Boolean> entries = new HashMap<>();
for (int i = 0; i < len; i++) {
ResolveInfo info = list.get(i);
CharSequence labelSeq = info.loadLabel(pm);
String label = labelSeq != null
? labelSeq.toString()
: info.activityInfo.name;
if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
String[] labelPath = label.split("/");
String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
addItem(myData, nextLabel, activityIntent(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name));
} else {
if (entries.get(nextLabel) == null) {
addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
entries.put(nextLabel, true);
}
}
}
}
Collections.sort(myData, sDisplayNameComparator);
return myData;
}
protected List<Map<String, Object>> getData(String prefix) {
List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
if (null == list)
return myData;
String[] prefixPath;
String prefixWithSlash = prefix;
if (prefix.equals("")) {
prefixPath = null;
} else {
prefixPath = prefix.split("/");
prefixWithSlash = prefix + "/";
}
int len = list.size();
Map<String, Boolean> entries = new HashMap<String, Boolean>();
for (int i = 0; i < len; i++) {
ResolveInfo info = list.get(i);
CharSequence labelSeq = info.loadLabel(pm);
String label = labelSeq != null
? labelSeq.toString()
: info.activityInfo.name;
if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
String[] labelPath = label.split("/");
String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
addItem(myData, nextLabel, activityIntent(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name));
} else {
if (entries.get(nextLabel) == null) {
addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
entries.put(nextLabel, true);
}
}
}
}
Collections.sort(myData, sDisplayNameComparator);
return myData;
}
private boolean isIntentAvailable(Intent intent)
{
final PackageManager packageManager = context.getPackageManager();
List list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}