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

最近由于項目需求,需要實現(xiàn)的功能大體如下:

借助QQ,微信的文件接收功能,使用戶在接收到文件之后可以跳轉到我們的App中,進行其他相關的業(yè)務.

好了不多說,直接上代碼:

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 類型

做完了上面的操作,已經(jīng)可以觸發(fā)其他應用的打開方式了,但是還不夠

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 - 分享內(nèi)容 - 接收其他APP的內(nèi)容

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,037評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • App 正在改變世界,豐富人們的生活,并為像您一樣的開發(fā)者提供前所未有的創(chuàng)新機會。因此,App Store 已成長...
    水中的藍天閱讀 1,728評論 0 5
  • 簡介 App 正在改變世界,豐富人們的生活,并為像您一樣的開發(fā)者提供前所未有的創(chuàng)新機會。因此,App Store ...
    o0_0o閱讀 3,726評論 2 48
  • 反思固化-21d 為一個值得堅持的行為,制定21天《“換出你的新習慣”挑戰(zhàn)表》 分值:3分
    打怪吧閱讀 128評論 1 0

友情鏈接更多精彩內(nèi)容