在Android中创建自定义的“打开方式”对话框

时间:2012-10-12 14:43:27

标签: android android-intent

单击文件时,“打开方式”对话框中会出现多个选项。这是因为我使用Action_VIEW发送了一个intent,并且有多个Apps可以接收它。

intent.setAction(android.content.Intent.ACTION_VIEW);
        Uri fileUri = Uri.parse("file://" + location);

出于安全原因,我想禁用其中一些,例如,用于从我的应用程序打开文件的打印机App。

是否有机制过滤掉选项或选择要显示的选项?

2 个答案:

答案 0 :(得分:0)

如果您知道应用程序名称,则可以尝试使用SetPackage。例如,在您的代码中,您可以使用setPackage

指定包名称
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setPackage("com.test.print");
Uri fileUri = Uri.parse("file://" + location);

答案 1 :(得分:0)

我正在回答我的问题,因为它可以让任何寻找类似东西的人受益。 因此,您可以自定义和创建自己的Open With Dialog:

第1步:获取您要显示的应用列表。为此,您需要从意图中筛选出应用程序。

final Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(fileUri, mimeType); 


List<ResolveInfo> getAllAppswhichcanViewtheFile =   (List<ResolveInfo>)getApplicationContext().getPackageManager().queryIntentActivities(intent, 0);

    Iterator<ResolveInfo> i = getAllAppswhichcanViewtheFile .iterator();
    while (i.hasNext()) {
        ResolveInfo file = i.next();

    // Use condition to filter the List 
        if (file.activityInfo.packageName.contains(XXXX)) {  
            i.next(); 
        } else {          
            i.remove();
        }

      List<ResolveInfo> resolvedPrintList = getAllAppswhichcanViewtheFile ;

步骤2:在警报对话框中显示已过滤的应用程序:

     AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(DataUtils.getString(R.string.xyz));
        final OpenWithArrayList adapter = new OpenWithArrayList(this,           R.layout.basiclistview, R.id.text1, resolvedPrintList);
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                ResolveInfo info = (ResolveInfo)adapter.getItem(which);              

intent1.setClassName(info.activityInfo.packageName,info.activityInfo.name);                     startActivity(intent1);
                }             });

        builder.create();
        builder.show();  

第3步:创建适配器: 公共类OpenWithArrayList扩展了ArrayAdapter {

    Context context;
    Object[] items;
    boolean[] arrows;
    int layoutId;





public OpenWithArrayList(Context context, int resource,
        int textViewResourceId, List<ResolveInfo> objects) {


    super(context, resource, textViewResourceId, objects);
    this.context = context;
    this.items = objects.toArray();
        this.layoutId = resource;

    // TODO Auto-generated constructor stub
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

     View row = convertView;

     if(row==null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      row=inflater.inflate(R.layout.basiclistview,parent, false);
      TextView label=(TextView)row.findViewById(R.id.text1);
     label.setText(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadLabel(context.getPackageManager()).toString());
     ImageView image = (ImageView) row.findViewById(R.id.logo);
     image.setImageDrawable(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadIcon(context.getPackageManager()));

     }


     return(row);

}

}