欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

android如何實(shí)現(xiàn)系統(tǒng)分享的自定義功能

小編給大家分享一下android如何實(shí)現(xiàn)系統(tǒng)分享的自定義功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)是專業(yè)的烏蘭察布網(wǎng)站建設(shè)公司,烏蘭察布接單;提供成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行烏蘭察布網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

分享功能是app中特別常見的功能,國內(nèi)的app基本都支持分享到微信 QQ等主流的社交應(yīng)用。至于分享功能的實(shí)現(xiàn)大多是使用第三方的share sdk一步到位,或者分享的app比較少比如就一個(gè)微信 那通常使用微信sdk的分享模塊即可。但其實(shí)android系統(tǒng)就給我們提供過一種分享的實(shí)現(xiàn)方式,代碼也比較簡單如下

Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("text/plain");
    share.putExtra(Intent.EXTRA_TEXT, shareText);
    share.putExtra(Intent.EXTRA_SUBJECT, shareSubject);

    if (uri != null) {
      share.setType("image/*");
      share.putExtra(Intent.EXTRA_STREAM, uri);
    }
    context.startActivity(Intent.createChooser(share, title));

android如何實(shí)現(xiàn)系統(tǒng)分享的自定義功能

系統(tǒng)提供的短短不到十行代碼,將分享列表 數(shù)據(jù) 展示 點(diǎn)擊 跳轉(zhuǎn) 跳轉(zhuǎn)后分享內(nèi)容的分享等一系列動作都集合完成了。這樣確實(shí)給人干凈利索的感覺,但隨之問題也來了比如我分享列表中只有特定幾個(gè)app,甚至把某個(gè)app放在第一個(gè),還有點(diǎn)擊Facebook的分享后分享方式我想用facebooksdk自帶的,等等一些列自定義功能完成就比較麻煩。

對個(gè)別app的分享特別處理

從以上的代碼可以看出,google官方定義出分享的key value一一對應(yīng)規(guī)則。比如Intent.EXTRA_STREAM對應(yīng)為分享圖片的uri,Intent.EXTRA_TEXT對應(yīng)為分享的text文本。從道理上講如果分享到的app都遵循google定義的這規(guī)則我們就能通過官方這代碼實(shí)現(xiàn)分享到所有app的功能。然而理想很豐滿現(xiàn)實(shí)很骨感,比如我們項(xiàng)目中要求分享到的facebook就壓根不遵守這規(guī)則,我們想實(shí)現(xiàn)分享到Facebook就必須用其sdk實(shí)現(xiàn)。于是我們就需要在分享列表中點(diǎn)擊Facebook是單獨(dú)走Facebook的分享邏輯。

  • 常規(guī)思維這是一個(gè)列表,我們監(jiān)聽列表item的點(diǎn)擊事件即可,然而從實(shí)現(xiàn)該分享列表的代碼 可以看出沒有類似listview recyclerview控件,也沒有adapter,扒了下源碼和google找不到item的點(diǎn)擊事件的監(jiān)聽,故該方案放棄了

  • 所以只能采用了自己實(shí)現(xiàn)分享列表,然后監(jiān)聽分享列表item點(diǎn)擊事件單獨(dú)處理的方式,也是符合常規(guī)思路

第一步:獲取分享列表的數(shù)據(jù)

private static List<List<ResolveInfo>> getShareActivities() {
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    PackageManager pm = App.getInstance().getPackageManager();
    List<List<ResolveInfo>> listArrayList = new ArrayList<>();
    List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
    List<ResolveInfo> newActivityList = new ArrayList<>();

    for (Iterator<ResolveInfo> it = activityList.iterator(); it.hasNext(); ) {
      ResolveInfo info = it.next();
      //過濾出facebook google+ whatapp twitter 分享app單獨(dú)處理
      LogUtils.e("+++", info.activityInfo.packageName);
      if (info.activityInfo.packageName.equals("com.android.bluetooth") || info.activityInfo.packageName.equals("com.android.nfc") || info.activityInfo.packageName.equals("com.facebook.katana") || info.activityInfo.packageName.equals("com.google.android.apps.plus") || info.activityInfo.packageName.equals("com.facebook.orca") || info.activityInfo.packageName.contains("whatsapp") || info.activityInfo.packageName.equals("com.twitter.android")) {
        if (info.activityInfo.packageName.equals("com.android.bluetooth") || info.activityInfo.packageName.equals("com.android.nfc")) {
          it.remove();
        } else {
          newActivityList.add(info);
          it.remove();
        }
      }
    }
    //增加一條other數(shù)據(jù)
    newActivityList.add(null);
    listArrayList.add(newActivityList);
    listArrayList.add(activityList);
    return listArrayList;
  }

第二步:構(gòu)建分享的列表,且定義點(diǎn)擊事件等

public static void systemShareDialog(List<ResolveInfo> packages, Context context, String title, String shareText) {
    List<Intent> targetIntents = new ArrayList<Intent>();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    SPM spm = new SPM();

    for (ResolveInfo candidate : packages) {
      String packageName = candidate.activityInfo.packageName;
      Intent target = new Intent(android.content.Intent.ACTION_SEND);
      target.setType("text/plain");
      target.putExtra(Intent.EXTRA_TEXT, shareText);
      //target.setPackage(packageName);
      //t will be able to handle the case of multiple activities within the same app that can handle this intent. Otherwise, a weird item of "Android System" will be shown
      target.setComponent(new ComponentName(packageName, candidate.activityInfo.name));

      targetIntents.add(target);
    }
//    createchooser時(shí)使用targetIntents.remove(0)即傳入targetIntents的第一個(gè)intent,并將其移除,
//
//    否則執(zhí)行chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[] {}));添加后啟動時(shí)會出現(xiàn)兩個(gè)相同的應(yīng)用
    Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), title);
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
    context.startActivity(chooserIntent);

  }

看完了這篇文章,相信你對“android如何實(shí)現(xiàn)系統(tǒng)分享的自定義功能”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前文章:android如何實(shí)現(xiàn)系統(tǒng)分享的自定義功能
鏈接URL:http://chinadenli.net/article34/ihhope.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)微信小程序全網(wǎng)營銷推廣網(wǎng)站收錄網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化