通过Intent打开手机中能够阅读word,excle或pdf的应用,让那个应用来打开文件。这里给出word,excle的open
OpenWord类
package com.example.tomorrow.MobileOA.utils; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; import java.io.File; import java.util.List; /** * Created by he on 11/10 2015.
* wang */ public class OpenWord { //param为文档的主要路径 public static Intent getWordFileIntent(String param) { Intent intent = null; try { intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/msword"); } catch (Exception e) { e.printStackTrace(); } return intent; } public static Intent getExcelFileIntent(File param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(param); intent.setDataAndType(uri, "application/vnd.ms-excel"); return intent; } /** * 判断Intent 是否存在 防止崩溃 * @param context * @param intent * @return */ public static boolean isIntentAvailable(Context context, Intent intent) { final PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES); return list.size() > 0; } }
为一个按钮绑定事件
bt_download_look.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = files.get(position).getName(); FileUtils fileUtils = new FileUtils(); String path = fileUtils.getSDPATH() + "MobileOA/" + name; Intent intent = OpenWord.getWordFileIntent(path); boolean flag = OpenWord.isIntentAvailable(context, intent); if (!flag) { Toast.makeText(context, "请安装offer", Toast.LENGTH_SHORT).show(); } else { context.startActivity(intent); } } });