最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 接收微信、QQ其他應(yīng)用打開第三方分享功能

 更新時間:2022年11月09日 09:20:51   作者:林恒  
這篇文章主要介紹了Android 接收微信、QQ其他應(yīng)用打開,第三方分享 ,思路很簡單通過在AndroidManifest.xml注冊ACTION事件,在用于接收分享的Activity里面加接收代碼,感興趣的朋友可以一起學(xué)習(xí)下

這里給大家分享我在網(wǎng)上總結(jié)出來的一些知識,希望對大家有所幫助

在AndroidManifest.xml注冊ACTION事件

<activity
        android:name="com.test.app.MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="這里的名稱會對外顯示"
        android:launchMode="singleTask"
        android:screenOrientation="portrait">
        //注冊接收分享
        <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>
             
        //注冊默認(rèn)打開事件,微信、QQ的其他應(yīng)用打開
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
             
            //接收打開的文件類型
            <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)程在后臺時,會調(diào)用Activity的onNewIntent方法
  • 當(dāng)APP進(jìn)程被殺死時,會調(diào)用onCreate方法

所以在兩個方法中都需要監(jiān)聽事件

@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需要對微信、QQ等第三方APP做兼容

  • 在文件管理選擇本應(yīng)用打開時,url的值為content://media/external/file/85139
  • 在微信中選擇本應(yīng)用打開時,url的值為 content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc
  • 在QQ中選擇本應(yīng)用打開時,url的值為 content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/

第一種為系統(tǒng)統(tǒng)一文件資源,能通過系統(tǒng)方法轉(zhuǎn)化為絕對路徑;
微信、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)用打開,第三方分享 的文章就介紹到這了,更多相關(guān)Android 接收微信QQ內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android ActivityManagerService啟動流程詳解

    Android ActivityManagerService啟動流程詳解

    這篇文章主要介紹了Android ActivityManagerService啟動流程,AMS,即ActivityManagerService,是安卓java framework的一個服務(wù),運(yùn)行在system_server進(jìn)程。此服務(wù)十分重要,因?yàn)樗芾碇沧康乃拇蠼M件,是安卓APP開發(fā)者最常接觸到的一個服務(wù)
    2023-02-02
  • Android 連接Wifi和創(chuàng)建Wifi熱點(diǎn)的實(shí)例

    Android 連接Wifi和創(chuàng)建Wifi熱點(diǎn)的實(shí)例

    本篇文章介紹了Android 連接Wifi和創(chuàng)建Wifi熱點(diǎn),小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。
    2016-10-10
  • Android ListView介紹及優(yōu)化方案

    Android ListView介紹及優(yōu)化方案

    這篇文章主要介紹了Android ListView介紹及優(yōu)化方案的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • OpenGL ES實(shí)現(xiàn)光照效果(六)

    OpenGL ES實(shí)現(xiàn)光照效果(六)

    這篇文章主要為大家詳細(xì)介紹了OpenGL ES實(shí)現(xiàn)光照效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android接入支付寶和微信支付的方法

    Android接入支付寶和微信支付的方法

    這篇文章主要介紹了Android接入支付寶和微信支付的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Android中解決RecyclerView各種點(diǎn)擊事件的方法

    Android中解決RecyclerView各種點(diǎn)擊事件的方法

    這篇文章主要介紹了Android中解決RecyclerView各種點(diǎn)擊事件的方法,完美解決RecyclerView點(diǎn)擊事件、長按事件、子項(xiàng)點(diǎn)擊事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android開發(fā)中DatePicker日期與時間控件實(shí)例代碼

    Android開發(fā)中DatePicker日期與時間控件實(shí)例代碼

    本文通過實(shí)例代碼給大家介紹了Android開發(fā)中DatePicker日期與時間控件,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-08-08
  • Android studio kotlin代碼格式化操作

    Android studio kotlin代碼格式化操作

    這篇文章主要介紹了Android studio kotlin代碼格式化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • 詳解Android單元測試方法與步驟

    詳解Android單元測試方法與步驟

    這篇文章給大家分享了Android單元測試方法與步驟的相關(guān)知識點(diǎn),有興趣和需要的朋友參考學(xué)習(xí)下。
    2018-07-07
  • Github簡單易用的?Android?ViewModel?Retrofit框架

    Github簡單易用的?Android?ViewModel?Retrofit框架

    這篇文章主要介紹了Github簡單易用的Android?ViewModel?Retrofit框架,RequestViewMode有自動對LiveData進(jìn)行緩存管理,每個retrofit api接口復(fù)用一個livedata的優(yōu)勢。下文具體詳情,感興趣的小伙伴可以參考一下
    2022-06-06

最新評論

专栏| 崇信县| 响水县| 策勒县| 五家渠市| 徐汇区| 灵宝市| 沅陵县| 焦作市| 曲周县| 台东县| 陆河县| 原平市| 银川市| 漳平市| 内乡县| 百色市| 辛集市| 鄱阳县| 临泉县| 英吉沙县| 来凤县| 宁陵县| 濉溪县| 万盛区| 祁连县| 新竹市| 大英县| 马龙县| 望谟县| 泽普县| 新安县| 孝感市| 伊川县| 油尖旺区| 台南市| 灵山县| 财经| 和静县| 平邑县| 北川|