我有一个列表,其中包含可以处理我的两个操作 Intent.ACTION_PICK
和 MediaStore.ACTION_IMAGE_CAPTURE
中的任何一个的应用程序。稍后我使用数组适配器使用此列表中的信息填充我的列表。
在我的 onItemClickListener
中,我能够从 ResolveInfo
实例获取 ActivityInfo
和 ComponentName
。但是我无法从 ResolveInfo
获取 IntentFilter
实例(尝试获取它会抛出 NullPointerException),现在 在我的列表中的任何应用必须解决我的一个 Intent 让它在列表中
这是抛出空指针异常的行 IntentFilter filter = launchable.filter;
private void acquirePicture(){
ListView lv=(ListView)dialog.findViewById(R.id.listView1);
PackageManager pm=getPackageManager();
List<ResolveInfo> launchables=pm.queryIntentActivityOptions(
this.getComponentName(),new Intent[]{takePicture},
photoPickerIntent,0);
Collections.sort(launchables,
new ResolveInfo.DisplayNameComparator(pm));
appAdapter=new AppAdapter(pm, launchables);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
ResolveInfo launchable=appAdapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
IntentFilter filter = launchable.filter;
if(filter.actionsIterator() != null){
Iterator<String > actions = filter.actionsIterator();
}
int actioncode;
Intent intent = new Intent();
Uri uri;
if(filter.hasAction(Intent.ACTION_PICK)){
actioncode = 1;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
intent.setData(uri);
}else{
actioncode = 0;
}
intent.setComponent(name);
startActivityForResult(intent,actioncode);
}
});
}
class AppAdapter extends ArrayAdapter<ResolveInfo> {
private PackageManager pm=null;
AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
super(Custom_chooser.this, R.layout.row, apps);
this.pm=pm;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
if (convertView==null) {
convertView=newView(parent);
}
bindView(position, convertView);
return(convertView);
}
private View newView(ViewGroup parent) {
return(getLayoutInflater().inflate(R.layout.row, parent, false));
}
private void bindView(int position, View row) {
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(getItem(position).loadLabel(pm));
ImageView icon=(ImageView)row.findViewById(R.id.icon);
icon.setImageDrawable(getItem(position).loadIcon(pm));
}
}
问题
如何在我的 onClickListner 正文中获取所选应用解析的 Intent ?
最佳答案
您需要明确说明您希望返回过滤器。而不是:
List<ResolveInfo> launchables=pm.queryIntentActivityOptions(
this.getComponentName(),
new Intent[]{takePicture}, photoPickerIntent, 0);
使用这个:
List<ResolveInfo> launchables=pm.queryIntentActivityOptions(
this.getComponentName(),
new Intent[]{takePicture}, photoPickerIntent,
PackageManager.GET_RESOLVED_FILTER);
我还没有实际测试过这个。请注意,存在许多与返回 IntentFilter
相关的错误和缺失的功能。在很多情况下,文档是错误的,在很多情况下,代码并没有按照您的预期执行,而且没有很多好的工作示例。
试试这个,如果有效请告诉我们。
关于android - 无法从 ResolveInfo 实例获取 Intent 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291986/