我正在创建一个选择器,我想添加一个我想要的选项,所以我必须删除exised选项。以下是我的代码:
private Intent createChooser(Intent targetintent){
Intent chooser=Intent.createChooser(targetintent, getString(R.string.choose_an_app));
PackageManager pm = getPackageManager();
List<ResolveInfo> infoList =pm.queryIntentActivities(targetintent, PackageManager.MATCH_DEFAULT_ONLY);
Log.i("TAG","size "+infoList.size());
List<LabeledIntent> labeledIntents = new ArrayList<>();
for(ResolveInfo ri:infoList){
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.providers.media")){//系统的 media stroage
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(RingtoneManager.ACTION_RINGTONE_PICKER);
labeledIntents.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
LabeledIntent[] extraIntents = labeledIntents.toArray( new LabeledIntent[ labeledIntents.size() ]);
Log.i("TAG","extarsize "+labeledIntents.size());
chooser.removeExtra(Intent.EXTRA_INITIAL_INTENTS);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,extraIntents);
return chooser;
}
我使用此代码
chooser.removeExtra(Intent.EXTRA_INITIAL_INTENTS);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,extraIntents);
删除现有选项,然后添加我的选项,但不能正常工作!
答案 0 :(得分:1)
无法从意图选择器中删除特定项目/选项。因此,创建自己的自定义列表视图/对话框,类似于带有目标选项的默认选择器对话框。
您已拥有目标包名称,您可以通过iconImageView.setImageDrawable(info.loadIcon(getPackageManager()));