android获取一个用于打开Word文件的intent

jopen 10年前

 下面贴一下主要代码:

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) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                return intent;            }  
获取意图后,判断该是否存在,防止崩溃:
    /**             * 判断Intent 是否存在 防止崩溃             *              * @param context             * @param intent             * @return             */            private boolean isIntentAvailable(Context context, Intent intent) {                final PackageManager packageManager = context.getPackageManager();                List<ResolveInfo> list = packageManager.queryIntentActivities(intent,                        PackageManager.GET_ACTIVITIES);                return list.size() > 0;            }  
如果list.size小于o,提示是否已经安装了office:
    Tools.showToast(NoticeDetailActivity.this,                        "请安装office");