我有一个列表,其中包含可以处理我的两个动作Intent.ACTION_PICK
和MediaStore.ACTION_IMAGE_CAPTURE
中的任何一个的应用程序。我后来使用数组适配器用此列表中的信息填充listivew。
在我的onItemClickListener
中,能够从ActivityInfo
实例中获取ComponentName
和ResolveInfo
。但是却无法从IntentFilter
中获取ResolveInfo
实例(试图使其抛出NullPointerException),并且还可以处理任何应用程序我列表中的内容必须已经解决了我的意图之一,使其成为列表中的内容
这是抛出nullpointerexception 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主体中获得所选应用程序解决的意图?
最佳答案
您需要明确地说要返回过滤器。代替:
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
有关,存在许多错误和缺少功能。在很多情况下,文档是错误的,在很多情况下,代码无法完成您期望的工作,并且没有很多好的示例。试试这个,让我们知道它是否有效。