Android gets a intent for opening Word files

Source: Internet
Author: User

Open the app on your phone with the ability to read word,excle or PDF via intent, and let that file open. This gives the word,excle open

Openword class

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 as the primary path to the document 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.VIE W "); 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; }/** * Determine if intent exists to prevent crashes * @param context * @param intent * @return */public static Boolean I SintentavaIlable (context context, Intent Intent) {final Packagemanager Packagemanager = Context.getpackagemanager (); list<resolveinfo> list = Packagemanager.queryintentactivities (Intent, packagemanager.get_activities) ; return list.size () > 0; }}

To bind an event to a button

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, "Please install offer", Toast.length_short). Show ();                } else {                    Context.startactivity (intent);}}        );

Android gets a intent for opening Word files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.