android系統(tǒng)分享的自定義功能的示例代碼
分享功能是app中特別常見的功能,國(guó)內(nèi)的app基本都支持分享到微信 QQ等主流的社交應(yīng)用。至于分享功能的實(shí)現(xiàn)大多是使用第三方的share sdk一步到位,或者分享的app比較少比如就一個(gè)微信 那通常使用微信sdk的分享模塊即可。但其實(shí)android系統(tǒng)就給我們提供過一種分享的實(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í)給人干凈利索的感覺,但隨之問題也來了比如我分享列表中只有特定幾個(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ī)則我們就能通過官方這代碼實(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[] {}));添加后啟動(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);
}
總結(jié)
至此完成了基于android系統(tǒng)提供的分享方式上的擴(kuò)展改造,自定義分享列表數(shù)據(jù)展示 自定義點(diǎn)擊后的分享形式等等都可以實(shí)現(xiàn)。。其實(shí)此功能中Intent.createChooser所做工作還是蠻多的,有興趣童鞋可以再去扒下起源碼,看具體實(shí)現(xiàn)和到底做了哪些工作。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android EditText限制輸入字符的方法總結(jié)
這篇文章主要介紹了 Android EditText限制輸入字符的方法總結(jié)的相關(guān)資料,這里提供了五種方法來實(shí)現(xiàn)并進(jìn)行比較,需要的朋友可以參考下2017-07-07
Android 通過代碼設(shè)置、打開wifi熱點(diǎn)及熱點(diǎn)連接的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 通過代碼設(shè)置、打開wifi熱點(diǎn)及熱點(diǎn)連接的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05
淺析Android系統(tǒng)的架構(gòu)以及程序項(xiàng)目的目錄結(jié)構(gòu)
這篇文章主要介紹了Android系統(tǒng)的架構(gòu)以及程序項(xiàng)目的目錄結(jié)構(gòu),是安卓開發(fā)入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-04-04
android之json數(shù)據(jù)過長(zhǎng)打印不全問題的解決
這篇文章主要介紹了android之json數(shù)據(jù)過長(zhǎng)打印不全問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04

