我正在尝试在浏览器中加载 Twitter URL。
在我的手机中,我也已经安装了 Twitter 应用程序。我正在尝试使用 ACTION_VIEW
Intent 打开 URL。但是发生的是,当我调用 intent 时,android 将显示默认的选择器对话框,其中还包含 twitter 应用程序(如果它安装在设备上)。我只想使用浏览器打开 URL。 我想从对话框中排除 Twitter 应用程序。我希望设备中所有可用的浏览器都显示在对话框中,而不是像 twitter、facebook 等 native 应用程序。
可不可以?如果可能的话,任何人都可以帮助我实现它。为了清楚起见,我还附上了我的代码和屏幕截图以及这个问题。
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);
最佳答案
我需要做类似的事情并找到了 this回答有帮助。我对其进行了修改,使其成为一个完整的解决方案:
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();
}
}
关于android - 如何从 ACTION_VIEW Intent 中排除特定应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18682833/