寫在Android開發者對Activities
在其他協議(例如ACTION_MAIN或ACTION_VIEW)中,如果您期望,可能無法獲得結果。
不能行動的意見返回你所期望的算,所以我所做的就是實現一個自定義警告對話框,顯示可以打開某個文件,略加修改所有可能的應用如下圖所示Custom intent chooser
對於那些想知道,它需要一個文件路徑作爲參數,並顯示你所有已安裝的應用程序可以通過獲取與fullpaths.Can的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<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);
}
}
我需要知道,如果一個用戶點擊某物,或關閉對話框 – sokie