zoukankan      html  css  js  c++  java
  • android获取一个用于打开Word文件的intent

     通过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);
                    }
                }
            });
    
  • 相关阅读:
    Mapreduce学习(一)
    Codeforces Global Round 10题解(A-D)
    八月第二周总结
    hdfs学习(三)
    Educational Codeforces Round 93 (Rated for Div. 2)题解
    hdfs学习(二)
    牛客团队赛50&CF#664(Div2)
    hdfs学习(一)
    蓝桥杯刷题(二)
    Kubernetes K8S之Pod 生命周期与postStart、preStop事件
  • 原文地址:https://www.cnblogs.com/wabi87547568/p/4966712.html
Copyright © 2011-2022 走看看