android intent actionview,android – 检查Intent.ACTION_VIEW上的用户操作

正如Android Developer在

Activities上所写

In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect.

您不能指望返回您期望的操作视图,所以我所做的是实现一个自定义警报对话框,显示可以打开某个文件的所有可能的应用程序,稍微修改后的版本,如此处所示Custom intent chooser

对于那些想知道的代码,它将filePath作为参数,并通过获取带有fullpath的mimetype.Works来显示所有已安装的应用程序,这些应用程序可以处理该文件类型.可以使用

AlertDialogIntentChooser alertDialog = new AlertDialogIntentChooser(filePath,getActivity());

alertDialog.show();

这是类,它可以采用可选的委托以及活动回调

public class AlertDialogIntentChooser {

private String filePath;

private Activity activity;

private AlertDialog dialog;

private AlertDialogDelegate delegate;

private ListItem[] items;

public AlertDialogIntentChooser(String filePath,Activity activity){

this.filePath = filePath;

this.activity = activity;

init();

}

public void setDialogDelegate(AlertDialogDelegate delegate){

this.delegate = delegate;

}

private void init(){

initApplicationItems();

AlertDialog.Builder builder = new AlertDialog.Builder(activity);

builder.setTitle(Strings.STRING_SELECT_APPLICATION);

builder.setIcon(R.drawable.ic_share);

builder.setOnCancelListener(new OnCancelListener() {

@Override

public void onCancel(DialogInterface paramDialogInterface) {

if(delegate!=null)

delegate.onDialogCancelled(paramDialogInterface);

}

});

builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Intent intentPdf = new Intent(Intent.ACTION_VIEW);

MimeTypeMap myMime = MimeTypeMap.getSingleton();

String fileExt = MimeTypeMap.getFileExtensionFromUrl(Uri.parse(filePath));

String mimeType = myMime.getMimeTypeFromExtension(fileExt);

intentPdf.setClassName(items[which].context, items[which].packageClassName);

intentPdf.setDataAndType(Uri.parse(filePath), mimeType);

try {

activity.startActivity(intentPdf);

dialog.dismiss();

if(delegate!=null)

delegate.onItemSelected(items[which].context, items[which].packageClassName);

}catch (ActivityNotFoundException e) {

Toast.makeText(activity,

Strings.ERROR_NO_VIEWER,

Toast.LENGTH_SHORT).show();

dialog.dismiss();

}

}

});

dialog = builder.create();

}

private void initApplicationItems(){

Intent intentPdf = new Intent(Intent.ACTION_VIEW);

MimeTypeMap myMime = MimeTypeMap.getSingleton();

String fileExt = MimeTypeMap.getFileExtensionFromUrl(Uri.parse(filePath));

String mimeType = myMime.getMimeTypeFromExtension(fileExt);

intentPdf.setDataAndType(Uri.parse(filePath), mimeType);

PackageManager pm = activity.getPackageManager();

List resInfos = pm.queryIntentActivities(intentPdf, 0);

items = new ListItem[resInfos.size()];

int i = 0;

for (ResolveInfo resInfo : resInfos) {

String context = resInfo.activityInfo.packageName;

String packageClassName = resInfo.activityInfo.name;

CharSequence label = resInfo.loadLabel(pm);

Drawable icon = resInfo.loadIcon(pm);

items[i] = new ListItem(label.toString(), icon, context, packageClassName);

++i;

}

}

public void show(){

dialog.show();

}

private ListAdapter adapter = new ArrayAdapter(

activity,

android.R.layout.select_dialog_item,

android.R.id.text1,

items){

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

View v = super.getView(position, convertView, parent);

TextView tv = (TextView)v.findViewById(android.R.id.text1);

int dpS = (int) (72 * activity.getResources().getDisplayMetrics().density * 0.5f);

items[position].icon.setBounds(0, 0, dpS, dpS);

tv.setCompoundDrawables(items[position].icon, null, null, null);

int dp5 = (int) (5 * activity.getResources().getDisplayMetrics().density * 0.5f);

tv.setCompoundDrawablePadding(dp5);

return v;

}

};

class ListItem {

public final String name;

public final Drawable icon;

public final String context;

public final String packageClassName;

public ListItem(String text, Drawable icon, String context, String packageClassName) {

this.name = text;

this.icon = icon;

this.context = context;

this.packageClassName = packageClassName;

}

@Override

public String toString() {

return name;

}

}

public static interface AlertDialogDelegate{

public void onDialogCancelled(DialogInterface paramDialogInterface);

public void onItemSelected(String packageName, String className);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值