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

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

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

這里給大家分享我在網(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="這里的名稱(chēng)會(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" />
 
            //接收分享的文件類(lèi)型
            <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)的文件類(lè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)文章

  • Android ActivityManagerService啟動(dòng)流程詳解

    Android ActivityManagerService啟動(dòng)流程詳解

    這篇文章主要介紹了Android ActivityManagerService啟動(dòng)流程,AMS,即ActivityManagerService,是安卓java framework的一個(gè)服務(wù),運(yùn)行在system_server進(jìn)程。此服務(wù)十分重要,因?yàn)樗芾碇沧康乃拇蠼M件,是安卓APP開(kāi)發(fā)者最常接觸到的一個(gè)服務(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),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
    2016-10-10
  • Android ListView介紹及優(yōu)化方案

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

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

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

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

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

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

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

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

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

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

    Android studio kotlin代碼格式化操作

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

    詳解Android單元測(cè)試方法與步驟

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

    Github簡(jiǎn)單易用的?Android?ViewModel?Retrofit框架

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

最新評(píng)論

都安| 商都县| 合作市| 红安县| 澄迈县| 潼南县| 南川市| 象州县| 饶平县| 石屏县| 城市| 岳西县| 南皮县| 伊通| 徐闻县| 广州市| 永丰县| 温泉县| 松阳县| 宁南县| 兰溪市| 万宁市| 广灵县| 会昌县| 剑川县| 赤峰市| 平南县| 筠连县| 奉化市| 盈江县| 六安市| 朝阳区| 枣强县| 凤庆县| 兴仁县| 蓬溪县| 兴安盟| 翁源县| 斗六市| 红河县| 保德县|