Android 接收微信、QQ其他應(yīng)用打開(kāi)第三方分享功能
這里給大家分享我在網(wǎng)上總結(jié)出來(lái)的一些知識(shí),希望對(duì)大家有所幫助
在AndroidManifest.xml注冊(cè)ACTION事件
<activity android:name="com.test.app.MainActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:label="這里的名稱會(huì)對(duì)外顯示" android:launchMode="singleTask" android:screenOrientation="portrait"> //注冊(cè)接收分享 <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> //接收分享的文件類型 <data android:mimeType="image/*" /> <data android:mimeType="application/msword" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" /> <data android:mimeType="application/vnd.ms-excel" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> <data android:mimeType="application/vnd.ms-powerpoint" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" /> <data android:mimeType="application/pdf" /> <data android:mimeType="text/plain" /> </intent-filter> //注冊(cè)默認(rèn)打開(kāi)事件,微信、QQ的其他應(yīng)用打開(kāi) <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> //接收打開(kāi)的文件類型 <data android:scheme="file" /> <data android:scheme="content" /> <data android:mimeType="image/*" /> <data android:mimeType="application/msword" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" /> <data android:mimeType="application/vnd.ms-excel" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> <data android:mimeType="application/vnd.ms-powerpoint" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" /> <data android:mimeType="application/pdf" /> <data android:mimeType="text/plain" /> </intent-filter> </activity>
在用于接收分享的Activity里面加接收代碼
- 當(dāng)APP進(jìn)程在后臺(tái)時(shí),會(huì)調(diào)用Activity的onNewIntent方法
- 當(dāng)APP進(jìn)程被殺死時(shí),會(huì)調(diào)用onCreate方法
所以在兩個(gè)方法中都需要監(jiān)聽(tīng)事件
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); receiveActionSend(intent); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); receiveActionSend(intent); }
receiveActionSend方法如下
public void receiveActionSend(Intent intent) { String action = intent.getAction(); String type = intent.getType(); //判斷action事件 if (type == null || (!Intent.ACTION_VIEW.equals(action) && !Intent.ACTION_SEND.equals(action))) { return; } //取出文件uri Uri uri = intent.getData(); if (uri == null) { uri = intent.getParcelableExtra(Intent.EXTRA_STREAM); } //獲取文件真實(shí)地址 String filePath = UriUtils.getFileFromUri(EdusohoApp.baseApp, uri); if (TextUtils.isEmpty(filePath)) { return; } //業(yè)務(wù)處理 . . . }
獲取真實(shí)路徑getFileFromUri方法
/** * 獲取真實(shí)路徑 * * @param context */ public static String getFileFromUri(Context context, Uri uri) { if (uri == null) { return null; } switch (uri.getScheme()) { case ContentResolver.SCHEME_CONTENT: //Android7.0之后的uri content:// URI return getFilePathFromContentUri(context, uri); case ContentResolver.SCHEME_FILE: default: //Android7.0之前的uri file:// return new File(uri.getPath()).getAbsolutePath(); } }
Android7.0之后的uri content:// URI需要對(duì)微信、QQ等第三方APP做兼容
- 在文件管理選擇本應(yīng)用打開(kāi)時(shí),url的值為content://media/external/file/85139
- 在微信中選擇本應(yīng)用打開(kāi)時(shí),url的值為 content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc
- 在QQ中選擇本應(yīng)用打開(kāi)時(shí),url的值為 content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/
第一種為系統(tǒng)統(tǒng)一文件資源,能通過(guò)系統(tǒng)方法轉(zhuǎn)化為絕對(duì)路徑;
微信、QQ的為fileProvider,只能獲取到文件流,需要先將文件copy到自己的私有目錄。
方法如下:
/** * 從uri獲取path * * @param uri content://media/external/file/109009 * <p> * FileProvider適配 * content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/ * content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/ */ private static String getFilePathFromContentUri(Context context, Uri uri) { if (null == uri) return null; String data = null; String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME}; Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(MediaStore.MediaColumns.DATA); if (index > -1) { data = cursor.getString(index); } else { int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME); String fileName = cursor.getString(nameIndex); data = getPathFromInputStreamUri(context, uri, fileName); } } cursor.close(); } return data; } /** * 用流拷貝文件一份到自己APP私有目錄下 * * @param context * @param uri * @param fileName */ private static String getPathFromInputStreamUri(Context context, Uri uri, String fileName) { InputStream inputStream = null; String filePath = null; if (uri.getAuthority() != null) { try { inputStream = context.getContentResolver().openInputStream(uri); File file = createTemporalFileFrom(context, inputStream, fileName); filePath = file.getPath(); } catch (Exception e) { } finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception e) { } } } return filePath; } private static File createTemporalFileFrom(Context context, InputStream inputStream, String fileName) throws IOException { File targetFile = null; if (inputStream != null) { int read; byte[] buffer = new byte[8 * 1024]; //自己定義拷貝文件路徑 targetFile = new File(context.getExternalCacheDir(), fileName); if (targetFile.exists()) { targetFile.delete(); } OutputStream outputStream = new FileOutputStream(targetFile); while ((read = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, read); } outputStream.flush(); try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return targetFile; }
到此這篇關(guān)于Android 接收微信、QQ其他應(yīng)用打開(kāi),第三方分享 的文章就介紹到這了,更多相關(guān)Android 接收微信QQ內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
下載、編譯、運(yùn)行android 7.1系統(tǒng)詳解(ubuntu 16.0.4)
Android 7的系統(tǒng)版本新增的很多的新功能,本篇文章主要介紹了基于ubuntu 16.0.4環(huán)境的下載、編譯、運(yùn)行android 7.1系統(tǒng),有興趣的可以了解一下。2017-01-01android使用DataBinding來(lái)設(shè)置空狀態(tài)
本篇文章主要介紹了android使用DataBinding來(lái)設(shè)置空狀態(tài),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android修改源碼解決Alertdialog觸摸對(duì)話框邊緣消失的問(wèn)題
在開(kāi)發(fā)的時(shí)候遇到一個(gè)問(wèn)題,就是一觸摸對(duì)話框邊緣外部,對(duì)話框會(huì)自動(dòng)消失。這個(gè)問(wèn)題很糾結(jié)啊,查找了一下發(fā)現(xiàn)從Android 4.0開(kāi)始,AlertDialog有了變化,就是在觸摸對(duì)話框邊緣外部,對(duì)話框會(huì)自動(dòng)消失,查了源碼,找到解決辦法如下2013-11-11Flutter WillPopScope攔截返回事件原理示例詳解
這篇文章主要為大家介紹了Flutter WillPopScope攔截返回事件原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android編程之SurfaceView學(xué)習(xí)示例詳解
這篇文章主要介紹了Android編程之SurfaceView學(xué)習(xí)示例,結(jié)合實(shí)例分析了SurfaceView的功能、使用方法與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android中imageview.ScaleType使用方法詳細(xì)介紹
這篇文章主要介紹了Android中imageview.ScaleType使用方法詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-06-06Android中FloatingActionButton的顯示與隱藏示例
本篇文章主要介紹了Android中FloatingActionButton的顯示與隱藏示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10Android瀑布流照片墻實(shí)現(xiàn) 體驗(yàn)不規(guī)則排列的美感
這篇文章主要為大家詳細(xì)介紹了Android瀑布流照片墻實(shí)現(xiàn),體驗(yàn)不規(guī)則排列的美感,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10