Refine search
Resources resources = getResources(); Intent emailIntent = new Intent(); emailIntent.setAction(Intent.ACTION_SEND); emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native))); emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject)); emailIntent.setType("message/rfc822"); List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0); List<LabeledIntent> intentList = new ArrayList<LabeledIntent>(); for (int i = 0; i < resInfo.size(); i++) { intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
@Override public int addIntentOptions(int group, int id, int categoryOrder, ComponentName caller, Intent[] specifics, Intent intent, int flags, android.view.MenuItem[] outSpecificItems) { PackageManager pm = mContext.getPackageManager(); final List<ResolveInfo> lri = pm.queryIntentActivityOptions(caller, specifics, intent, 0); final int N = lri != null ? lri.size() : 0; if ((flags & FLAG_APPEND_TO_GROUP) == 0) { removeGroup(group); } for (int i = 0; i < N; i++) { final ResolveInfo ri = lri.get(i); Intent rintent = new Intent( ri.specificIndex < 0 ? intent : specifics[ri.specificIndex]); rintent.setComponent(new ComponentName( ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name)); final android.view.MenuItem item = add(group, id, categoryOrder, ri.loadLabel(pm)) .setIcon(ri.loadIcon(pm)) .setIntent(rintent); if (outSpecificItems != null && ri.specificIndex >= 0) { outSpecificItems[ri.specificIndex] = item; } } return N; }
@Test public void resolveActivity_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.activityInfo = new ActivityInfo(); info.activityInfo.name = "name"; info.activityInfo.packageName = TEST_PACKAGE_NAME; shadowPackageManager.addResolveInfoForIntent(i, info); assertThat(packageManager.resolveActivity(i, 0)).isNotNull(); assertThat(packageManager.resolveActivity(i, 0).activityInfo.name).isEqualTo("name"); assertThat(packageManager.resolveActivity(i, 0).activityInfo.packageName) .isEqualTo(TEST_PACKAGE_NAME); }
Drawable loadIconForResolveInfo(ResolveInfo ri) { Drawable dr; try { if (ri.resolvePackageName != null && ri.icon != 0) { dr = getIcon(mPm.getResourcesForApplication(ri.resolvePackageName), ri.icon); if (dr != null) { return dr; } } final int iconRes = ri.getIconResource(); if (iconRes != 0) { dr = getIcon(mPm.getResourcesForApplication(ri.activityInfo.packageName), iconRes); if (dr != null) { return dr; } } } catch (PackageManager.NameNotFoundException e) { VLog.e(TAG, "Couldn't find resources for package\n" + VLog.getStackTraceString(e)); } return ri.loadIcon(mPm); }
/** * 获取系统安装的APP应用 * * @param context */ public static ArrayList<AppInfo> getAllApp(Context context) { PackageManager manager = context.getPackageManager(); Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0); // 将获取到的APP的信息按名字进行排序 Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager)); ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); for (ResolveInfo info : apps) { AppInfo appInfo = new AppInfo(); appInfo.setAppLable(info.loadLabel(manager) + ""); appInfo.setAppIcon(info.loadIcon(manager)); appInfo.setAppPackage(info.activityInfo.packageName); appInfo.setAppClass(info.activityInfo.name); appList.add(appInfo); System.out.println("info.activityInfo.packageName=" + info.activityInfo.packageName); System.out.println("info.activityInfo.name=" + info.activityInfo.name); } return appList; }
PackageManager pm = kyoPrint.getPackageManager(); Intent viewIntent = new Intent(Intent.ACTION_VIEW); Intent editIntent = new Intent(Intent.ACTION_EDIT); viewIntent.setDataAndType(uri, type); editIntent.setDataAndType(uri, type); Intent openInChooser = Intent.createChooser(viewIntent, "Open in..."); // Append " (for editing)" to applicable apps, otherwise they will show up twice identically Spannable forEditing = new SpannableString(" (for editing)"); forEditing.setSpan(new ForegroundColorSpan(Color.CYAN), 0, forEditing.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); List<ResolveInfo> resInfo = pm.queryIntentActivities(editIntent, 0); Intent[] extraIntents = new Intent[resInfo.size()]; for (int i = 0; i < resInfo.size(); i++) { // Extract the label, append it, and repackage it in a LabeledIntent ResolveInfo ri = resInfo.get(i); String packageName = ri.activityInfo.packageName; Intent intent = new Intent(); intent.setComponent(new ComponentName(packageName, ri.activityInfo.name)); intent.setAction(Intent.ACTION_EDIT); intent.setDataAndType(uri, type); CharSequence label = TextUtils.concat(ri.loadLabel(pm), forEditing); extraIntents[i] = new LabeledIntent(intent, packageName, label, ri.icon); } openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); startActivity(openInChooser);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); sendIntent.setType("image/jpeg"); List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0); List<LabeledIntent> intentList = new ArrayList<>(); ResolveInfo ri = resInfo.get(i); String packageName = ri.activityInfo.packageName; final Intent intent = new Intent(); intent.setComponent(new ComponentName(packageName, ri.activityInfo.name)); intent.setPackage(packageName); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); intent.setType("image/jpeg"); intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.getIconResource()));
@Test @Config(minSdk = LOLLIPOP) public void queryIntentContentProviders_Match() throws Exception { Intent i = new Intent(DocumentsContract.PROVIDER_INTERFACE); ResolveInfo resolveInfo = new ResolveInfo(); ProviderInfo providerInfo = new ProviderInfo(); providerInfo.authority = "com.robolectric"; resolveInfo.providerInfo = providerInfo; shadowPackageManager.addResolveInfoForIntent(i, resolveInfo); List<ResolveInfo> contentProviders = packageManager.queryIntentContentProviders(i, 0); assertThat(contentProviders).hasSize(1); assertThat(contentProviders.get(0).providerInfo.authority).isEqualTo(providerInfo.authority); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void tryNewMiuiBadge(Context context, int badgeCount) throws ShortcutBadgeException { if (resolveInfo == null) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); } if (resolveInfo != null) { NotificationManager mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(context) .setContentTitle("") .setContentText("") .setSmallIcon(resolveInfo.getIconResource()); Notification notification = builder.build(); try { Field field = notification.getClass().getDeclaredField("extraNotification"); Object extraNotification = field.get(notification); Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class); method.invoke(extraNotification, badgeCount); mNotificationManager.notify(0, notification); } catch (Exception e) { throw new ShortcutBadgeException("not able to set badge", e); } } }
@Test public void isServiceAvailable() throws InterruptedException, RemoteException { Intent serviceIntent = new Intent(Constants.ACTION_BILLING_SERVICE_BIND); serviceIntent.setPackage(Constants.VENDING_PACKAGE); List<ResolveInfo> list = new ArrayList<>(); list.add(new ResolveInfo()); Context context = mock(Context.class); PackageManager packageManager = mock(PackageManager.class); when(context.getPackageManager()).thenReturn(packageManager); when(packageManager.queryIntentServices(any(Intent.class), eq(0))).thenReturn(list); assertThat(BillingProcessorObservable.isServiceAvailable(context)).isTrue(); }
@Test public void testLaunchIntentForPackage() { Intent intent = packageManager.getLaunchIntentForPackage(TEST_PACKAGE_LABEL); assertThat(intent).isNull(); Intent launchIntent = new Intent(Intent.ACTION_MAIN); launchIntent.setPackage(TEST_PACKAGE_LABEL); launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.activityInfo = new ActivityInfo(); resolveInfo.activityInfo.packageName = TEST_PACKAGE_LABEL; resolveInfo.activityInfo.name = "LauncherActivity"; shadowPackageManager.addResolveInfoForIntent(launchIntent, resolveInfo); intent = packageManager.getLaunchIntentForPackage(TEST_PACKAGE_LABEL); assertThat(intent.getComponent().getClassName()).isEqualTo("LauncherActivity"); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PackageManager mgr=getPackageManager(); Intent i= new Intent(Intent.ACTION_VIEW, Uri.parse("https://commonsware.com")); ResolveInfo ri= mgr.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY); Toast.makeText(this, ri.loadLabel(mgr), Toast.LENGTH_LONG).show(); startActivity(i); finish(); } }
@Test public void testDownloadPdf() { ResolveInfo resolverInfo = new ResolveInfo(); resolverInfo.activityInfo = new ActivityInfo(); resolverInfo.activityInfo.applicationInfo = new ApplicationInfo(); resolverInfo.activityInfo.applicationInfo.packageName = ListActivity.class.getPackage().getName(); resolverInfo.activityInfo.name = ListActivity.class.getName(); ShadowPackageManager rpm = shadowOf(RuntimeEnvironment.application.getPackageManager()); when(item.getUrl()).thenReturn("http://example.com/file.pdf"); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(item.getUrl())), resolverInfo); WebView webView = activity.findViewById(R.id.web_view); ShadowWebView shadowWebView = Shadow.extract(webView); WebFragment fragment = (WebFragment) activity.getSupportFragmentManager() .findFragmentByTag(WebFragment.class.getName()); shadowWebView.getDownloadListener().onDownloadStart(item.getUrl(), "", "", "application/pdf", 0L); shadowWebView.getWebViewClient().onPageFinished(webView, PDF_LOADER_URL); verify(fragment.mFileDownloader).downloadFile( eq(item.getUrl()), eq("application/pdf"), any(FileDownloader.FileDownloaderCallback.class)); }
public static Drawable resolveFileTypeIcon(@NonNull Context ctx, Uri fileUri) { final Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(fileUri); intent.setType(getMimeType(ctx, fileUri)); final PackageManager pm = ctx.getPackageManager(); final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0); for (ResolveInfo match : matches) { //final CharSequence label = match.loadLabel(pm); return match.loadIcon(pm); } return null; //ContextCompat.getDrawable(ctx, R.drawable.ic_file); }
@Test public void queryIntentActivities_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null); i.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.activityInfo = new ActivityInfo(); info.activityInfo.name = "name"; info.activityInfo.packageName = TEST_PACKAGE_NAME; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> activities = packageManager.queryIntentActivities(i, 0); assertThat(activities).isNotNull(); assertThat(activities).hasSize(2); assertThat(activities.get(0).nonLocalizedLabel.toString()).isEqualTo(TEST_PACKAGE_LABEL); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void queryIntentActivitiesAsUser_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null); i.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> activities = packageManager.queryIntentActivitiesAsUser(i, 0, 0); assertThat(activities).isNotNull(); assertThat(activities).hasSize(2); assertThat(activities.get(0).nonLocalizedLabel.toString()).isEqualTo(TEST_PACKAGE_LABEL); }
@Test public void removeResolveInfosForIntent_forService() throws Exception { Intent intent = new Intent(Intent.ACTION_APP_ERROR, null).addCategory(Intent.CATEGORY_APP_BROWSER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.serviceInfo = new ServiceInfo(); info.serviceInfo.packageName = "com.org"; shadowPackageManager.addResolveInfoForIntent(intent, info); shadowPackageManager.removeResolveInfosForIntent(intent, "com.org"); assertThat(packageManager.resolveService(intent, 0)).isNull(); }
public static List<AutoStartInfo> fetchAutoApps(Context mContext) { PackageManager pm = mContext.getPackageManager(); Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); List<ResolveInfo> resolveInfoList = pm.queryBroadcastReceivers(intent, PackageManager.GET_DISABLED_COMPONENTS); List<AutoStartInfo> appList = new ArrayList<AutoStartInfo>(); String appName = null; if (resolveInfoList.size() > 0) { appName = resolveInfoList.get(0).loadLabel(pm).toString(); packageReceiver = resolveInfoList.get(0).activityInfo.packageName + "/" + resolveInfoList.get(0).activityInfo.name; icon = resolveInfoList.get(0).loadIcon(pm); ComponentName mComponentName1 = new ComponentName(resolveInfoList.get(0).activityInfo.packageName, resolveInfoList.get(0).activityInfo.name); if (pm.getComponentEnabledSetting(mComponentName1) == 2) { if (appName.equals(resolveInfoList.get(i).loadLabel(pm).toString())) { packageReceiver = packageReceiver + ";" + resolveInfoList.get(i).activityInfo.packageName + "/" + resolveInfoList.get(i).activityInfo.name; } else { appName = resolveInfoList.get(i).loadLabel(pm).toString(); packageReceiver = resolveInfoList.get(i).activityInfo.packageName + "/" + resolveInfoList.get(i).activityInfo.name; icon = resolveInfoList.get(i).loadIcon(pm); ComponentName mComponentName2 = new ComponentName(resolveInfoList.get(i).activityInfo.packageName, resolveInfoList.get(i).activityInfo.name); if (pm.getComponentEnabledSetting(mComponentName2) == 2) {
@Test public void queryIntentActivities_ServiceMatch() throws Exception { Intent i = new Intent("SomeStrangeAction"); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.serviceInfo = new ServiceInfo(); info.serviceInfo.name = "name"; info.serviceInfo.packageName = TEST_PACKAGE_NAME; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> activities = packageManager.queryIntentActivities(i, 0); assertThat(activities).isNotNull(); assertThat(activities).isEmpty(); }