我使用Intent.createChooser()
方法打开选择器对话框。但是在该对话框中,我看不到“仅一次”和“总是”选项。
我想要使用Intent.createChooser()
方法的这种选择对话框
这是我的代码。
File mFile = new File("give your file path here");
String type = "video/mp4";
Intent viewDoc = new Intent(Intent.ACTION_VIEW);
viewDoc.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
viewDoc.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(this, "com.xyz.fileprovider", mFile);
viewDoc.setDataAndType(contentUri, type);
PackageManager pm = getPackageManager();
apps = pm.queryIntentActivities(viewDoc, PackageManager.MATCH_DEFAULT_ONLY);
ArrayList<Intent> targetIntents = new ArrayList<Intent>();
for (ResolveInfo resolveInfo : apps) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!BuildConfig.APPLICATION_ID.equals(packageName)) {
Intent targetIntent = new Intent(Intent.ACTION_VIEW); // targetIntent.setDataAndType(contentUri, type);
targetIntent.setPackage(packageName);
targetIntent.setDataAndType(contentUri, type);
targetIntents.add(targetIntent);
}
}
if (type == null || type.equals("") || targetIntents.size() <= 0) { //application/octet-stream
android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(this).create();
alertDialog.setTitle(getResources().getString(R.string.app_name));
alertDialog.setMessage(getResources().getString(R.string.offline_file_not_supported_error));
alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
} else {
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Open file with");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
//startActivity(viewDoc);
}
但是在此对话框中,我只需要一次且始终选择。
我使用了下面提到的代码,因为我想从选择器中删除自己的应用。
for (ResolveInfo resolveInfo : apps) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!BuildConfig.APPLICATION_ID.equals(packageName)) {
Intent targetIntent = new Intent(Intent.ACTION_VIEW); // targetIntent.setDataAndType(contentUri, type);
targetIntent.setPackage(packageName);
targetIntent.setDataAndType(contentUri, type);
targetIntents.add(targetIntent);
}
}