检查Intent.ACTION_VIEW用户操作 [英] Check user action on Intent.ACTION_VIEW
问题描述
我有填充了一些文件的列表视图,可以有不同的类型,如PDF或documents.When用户点击一个我得到的文件MIME类型和启动,它可以让用户选择使用打开哪个应用程序的意图file.What我想是要知道的是用户选用的东西,或者干脆pressed回来,没有选择任何东西。 我尝试过,直到现在在做一个startActivityForResult并检查成功,但它总是返回RESULT_CANCELED
I have a listview populated with some files,there can be various types like pdf or documents.When a user clicks on one i get the file mime type and start an intent that let's the user choose which application to use to open that file.What i want is to know is a user choosed something,or simply pressed back and didn't choose anything. What i tried untill now was doing a startActivityForResult and checking for success,but it returns always RESULT_CANCELED
static final int SELECTED_VIEWER = 1;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(filePath), mimetype);
try {
startActivityForResult(intent, SELECTED_VIEWER);
}catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(),
Strings.ERROR_NO_VIEWER,
Toast.LENGTH_SHORT).show();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECTED_VIEWER) {
if (resultCode == Activity.RESULT_CANCELED ) {
//do something
}
}
}
我甚至尝试用startActivityForResult(Intent.createChooser,但仍无济于事。 我怎么能知道,如果用户选用的东西就行了,或者他取消了开?
I even tried with an startActivityForResult(Intent.createChooser but still to no avail. How can i know if the user choosed something on that list,or if he cancelled the open?
推荐答案
写在Android开发的<一个href="http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int,%20android.os.Bundle%29">Activities
As written in Android Developer on Activities
在其他协议(如ACTION_MAIN或ACTION_VIEW),你可能不会得到结果,当你想到的。
In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect.
您可以在行动景色,然后回到你所期望的不算,所以我所做的就是实现这表明,可以打开一个特定的文件,一个稍微修改后的版本如下所示的Custom意图选择器
You can't count on action views returning what you would expect,so what i did was implement a custom alert dialog that shows all possible applications that can open a certain file,a slightly modified version as shown here Custom intent chooser
code对于那些想知道,它需要一个文件路径作为参数,并显示你所有已安装的应用程序,可以通过获取与fullpaths.Can的mimetype.Works处理该文件类型来调用
Code for those wondering,it takes a filePath as parameter and shows you all installed applications that can handle that filetype by getting the mimetype.Works with fullpaths.Can be called with
AlertDialogIntentChooser alertDialog = new AlertDialogIntentChooser(filePath,getActivity());
alertDialog.show();
这是类,它可以采取一个可选的委托藏汉的活动回调
this is the class,it can take an optional delegate aswell for activity callbacks
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<ResolveInfo> 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<ListItem>(
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);
}
}
这篇关于检查Intent.ACTION_VIEW用户操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!