QQ微信,其他方式打开文档(doc,docx,ppt,pptx,xls,xlsx,pdf)到自己的App

最近由于项目需求,需要实现的功能大体如下:

借助QQ,微信的文件接收功能,使用户在接收到文件之后可以跳转到我们的App中,进行其他相关的业务.

好了不多说,直接上代码:

1.首先需要在AndroidManifest.xml中声明

     <activity android:name={ActivityName}>
        <!--doc-->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/msword"/>
        </intent-filter>
        <!--pdf-->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/pdf"/>
        </intent-filter>
        <!--ppt-->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/vnd.ms-powerpoint"/>
        </intent-filter>
        <!--xls-->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/vnd.ms-excel"/>
        </intent-filter>
        <!--xlsx-->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
        </intent-filter>
        <!--docx-->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document"/>
        </intent-filter>
        <!--pptx-->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation"/>
        </intent-filter>
    </activity>

声明的作用:告诉其他的app你可以(View)打开这类的文件,而具体是哪一类文件,借助Action但关键还是借助 MIME 类型

做完了上面的操作,已经可以触发其他应用的打开方式了,但是还不够

qq.png
第四个就是本公司app(打广告).png

2.到声明的Activity下接受其他App传递的消息

void onCreate (Bundle savedInstanceState) {
    ...
    // 获得 intent, action 和 MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_VIEW.equals(action) && type != null) {
        if ("application/msword".equals(type)) {
            handle_Doc(intent); // 处理doc
        }
        ...
    }
}

private void handle_Doc(Intent intent) {
    Uri data = intent.getData();
    String path = data.getPath();//文件路径
    ...
}
雨有点大

关于这一功能的参考文档:

MIME 参考手册 - W3School
Android - 分享内容 - 接收其他APP的内容

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,323评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,120评论 19 139
  • App 正在改变世界,丰富人们的生活,并为像您一样的开发者提供前所未有的创新机会。因此,App Store 已成长...
    水中的蓝天阅读 5,577评论 0 5
  • 简介 App 正在改变世界,丰富人们的生活,并为像您一样的开发者提供前所未有的创新机会。因此,App Store ...
    o0_0o阅读 8,939评论 2 48
  • 反思固化-21d 为一个值得坚持的行为,制定21天《“换出你的新习惯”挑战表》 分值:3分
    打怪吧阅读 1,108评论 1 0

友情链接更多精彩内容