小編給大家分享一下android如何實(shí)現(xiàn)系統(tǒng)分享的自定義功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)是專(zhuān)業(yè)的烏蘭察布網(wǎng)站建設(shè)公司,烏蘭察布接單;提供成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行烏蘭察布網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
分享功能是app中特別常見(jiàn)的功能,國(guó)內(nèi)的app基本都支持分享到微信 QQ等主流的社交應(yīng)用。至于分享功能的實(shí)現(xiàn)大多是使用第三方的share sdk一步到位,或者分享的app比較少比如就一個(gè)微信 那通常使用微信sdk的分享模塊即可。但其實(shí)android系統(tǒng)就給我們提供過(guò)一種分享的實(shí)現(xiàn)方式,代碼也比較簡(jiǎ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));
系統(tǒng)提供的短短不到十行代碼,將分享列表 數(shù)據(jù) 展示 點(diǎn)擊 跳轉(zhuǎn) 跳轉(zhuǎn)后分享內(nèi)容的分享等一系列動(dòng)作都集合完成了。這樣確實(shí)給人干凈利索的感覺(jué),但隨之問(wèn)題也來(lái)了比如我分享列表中只有特定幾個(gè)app,甚至把某個(gè)app放在第一個(gè),還有點(diǎn)擊Facebook的分享后分享方式我想用facebooksdk自帶的,等等一些列自定義功能完成就比較麻煩。
對(duì)個(gè)別app的分享特別處理
從以上的代碼可以看出,google官方定義出分享的key value一一對(duì)應(yīng)規(guī)則。比如Intent.EXTRA_STREAM對(duì)應(yīng)為分享圖片的uri,Intent.EXTRA_TEXT對(duì)應(yīng)為分享的text文本。從道理上講如果分享到的app都遵循google定義的這規(guī)則我們就能通過(guò)官方這代碼實(shí)現(xiàn)分享到所有app的功能。然而理想很豐滿(mǎn)現(xiàn)實(shí)很骨感,比如我們項(xiàng)目中要求分享到的facebook就壓根不遵守這規(guī)則,我們想實(shí)現(xiàn)分享到Facebook就必須用其sdk實(shí)現(xiàn)。于是我們就需要在分享列表中點(diǎn)擊Facebook是單獨(dú)走Facebook的分享邏輯。
常規(guī)思維這是一個(gè)列表,我們監(jiān)聽(tīng)列表item的點(diǎn)擊事件即可,然而從實(shí)現(xiàn)該分享列表的代碼 可以看出沒(méi)有類(lèi)似listview recyclerview控件,也沒(méi)有adapter,扒了下源碼和google找不到item的點(diǎn)擊事件的監(jiān)聽(tīng),故該方案放棄了
所以只能采用了自己實(shí)現(xiàn)分享列表,然后監(jiān)聽(tīng)分享列表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(); //過(guò)濾出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[] {}));添加后啟動(dòng)時(shí)會(huì)出現(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); }
看完了這篇文章,相信你對(duì)“android如何實(shí)現(xiàn)系統(tǒng)分享的自定義功能”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前文章:android如何實(shí)現(xiàn)系統(tǒng)分享的自定義功能
鏈接URL:http://aaarwkj.com/article34/ihhope.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、軟件開(kāi)發(fā)、微信小程序、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站收錄、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)