如何从ACTION_VIEW Intent中排除特定的应用程序?

San*_*ese 12 android android-intent

我正在尝试在浏览器中加载Twitter URL.

在我的手机中,我已经安装了twitter应用程序.我正在尝试使用ACTION_VIEWintent 打开URL .但是,当我调用intent时,android会显示默认的选择器对话框,其中也包含twitter应用程序(如果它安装在设备上).我想只使用浏览器打开URL.我想从对话框中排除Twitter应用程序.我希望设备中的所有可用浏览器都显示在对话框中,而不是像twitter,facebook等本机应用程序.

有可能吗?如果可能的话,任何人都可以帮助我实现它.为了清楚起见,我还附上了我的代码和截图以及这个问题.

String url = "https://twitter.com";
MimeTypeMap map = MimeTypeMap.getSingleton();
String type = map.getMimeTypeFromExtension(url);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setType(type);
i.setData(Uri.parse(url));
startActivity(i);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Suf*_*ian 17

我需要做类似的事情,发现这个答案很有帮助.我已对其进行了修改,以便它是一个完整的解决方案:

public static void openFileWithInstalledAppExceptCurrentApp(File file, final Activity activity) {
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String ext = file.getName().substring(file.getName().indexOf(".")+1);
    String type = mime.getMimeTypeFromExtension(ext);
    intent.setDataAndType(Uri.fromFile(file),type);
    PackageManager packageManager = activity.getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
    String packageNameToHide = "com.test.app";
    ArrayList<Intent> targetIntents = new ArrayList<Intent>();
    for (ResolveInfo currentInfo : activities) {
            String packageName = currentInfo.activityInfo.packageName;
        if (!packageNameToHide.equals(packageName)) {
            Intent targetIntent = new Intent(android.content.Intent.ACTION_VIEW);
            targetIntent.setDataAndType(Uri.fromFile(file),type);
            targetIntent.setPackage(packageName);
            targetIntents.add(targetIntent);
        }
    }
    if(targetIntents.size() > 0) {
        Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Open file with");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[] {}));
        activity.startActivity(chooserIntent);
    }
    else {
        Toast.makeText(this, "No app found", Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 你在`packageNameToHide.equals(packageName)之前忘了一个`!`.现在它始终打开packageNameOfAppToHide. (2认同)
  • 我当前的应用仍在选择器中列出。我必须添加此行来修复它:`targetIntent.setComponent(new ComponentName(packageName,currentInfo.activityInfo.name));` (2认同)

flx*_*flx 4

您只需将目标包设置为意图即可:

String url = "https://twitter.com";
MimeTypeMap map = MimeTypeMap.getSingleton();
String type = map.getMimeTypeFromExtension(url);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setType(type);
i.setData(Uri.parse(url));
ComponentName comp = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
i.setComponent(comp);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Run Code Online (Sandbox Code Playgroud)

但是,如果用户安装了某些自定义浏览器并希望将其用作默认浏览器,他们可能会感到不安。

您可以尝试使用以下方法检测默认浏览器:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com"));
List<ResolveInfo> list = context.getPackageManager()
    .queryIntentActivities(i, 0);
// walk through list and select your most favorite browser
Run Code Online (Sandbox Code Playgroud)