Android編程實(shí)現(xiàn)自定義分享列表ACTION_SEND功能的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)自定義分享列表ACTION_SEND功能的方法。分享給大家供大家參考,具體如下:
看到最近都在做自定義的東西,因?yàn)楸容^靈活,還可以擺脫系統(tǒng)自身不怎么漂亮的UI,(大家都懂得)所以自己也做了下自定義的分享列表,用PopupWindow的方式彈出。
先上效果圖:
1、布局:
popup_share.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ListView android:id="@+id/share_list" android:background="#2F4F4F" android:fadingEdge="none" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:divider="#E2DD75" android:dividerHeight="1.0dip" android:headerDividersEnabled="true" android:footerDividersEnabled="false" /> </LinearLayout>
popup_share_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="2.0dip" > <ImageView android:id="@+id/share_item_icon" android:layout_width="32.0dip" android:layout_height="32.0dip" android:layout_marginLeft="3.0dip" android:scaleType="fitXY" /> <TextView android:id="@+id/share_item_name" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="分享" android:textColor="@color/white" android:singleLine="true" android:textSize="@dimen/s_size" android:layout_marginLeft="3.0dip" android:layout_marginRight="3.0dip" /> </LinearLayout>
2、查詢手機(jī)內(nèi)所有支持分享的應(yīng)用列表
public List<ResolveInfo> getShareApps(Context context) { List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); Intent intent = new Intent(Intent.ACTION_SEND, null); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("text/plain"); // intent.setType("*/*"); PackageManager pManager = context.getPackageManager(); mApps = pManager.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); return mApps; }
注:ApplicationInfo是從一個(gè)特定的應(yīng)用得到的信息。這些信息是從相對(duì)應(yīng)的Androdimanifest.xml的< application>標(biāo)簽中收集到的。
ResolveInfo這個(gè)類(lèi)是通過(guò)解析一個(gè)與IntentFilter相對(duì)應(yīng)的intent得到的信息。它部分地對(duì)應(yīng)于從AndroidManifest.xml的< intent>標(biāo)簽收集到的信息。
得到List列表,我自建的AppInfo類(lèi),自己建一個(gè)就行
private List<AppInfo> getShareAppList() { List<AppInfo> shareAppInfos = new ArrayList<AppInfo>(); PackageManager packageManager = getPackageManager(); List<ResolveInfo> resolveInfos = getShareApps(mContext); if (null == resolveInfos) { return null; } else { for (ResolveInfo resolveInfo : resolveInfos) { AppInfo appInfo = new AppInfo(); appInfo.setAppPkgName(resolveInfo.activityInfo.packageName); // showLog_I(TAG, "pkg>" + resolveInfo.activityInfo.packageName + ";name>" + resolveInfo.activityInfo.name); appInfo.setAppLauncherClassName(resolveInfo.activityInfo.name); appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString()); appInfo.setAppIcon(resolveInfo.loadIcon(packageManager)); shareAppInfos.add(appInfo); } } return shareAppInfos; }
3、彈出PopupWindow的實(shí)現(xiàn)
private void initSharePopupWindow(View parent) { PopupWindow sharePopupWindow = null; View view = null; ListView shareList = null; if(null == sharePopupWindow) { //加載布局文件 view = LayoutInflater.from(DetailExchangeActivity.this).inflate(R.layout.popup_share, null); shareList = (ListView) view.findViewById(R.id.share_list); List<AppInfo> shareAppInfos = getShareAppList(); final ShareCustomAdapter adapter = new ShareCustomAdapter(mContext, shareAppInfos); shareList.setAdapter(adapter); shareList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Intent shareIntent = new Intent(Intent.ACTION_SEND); AppInfo appInfo = (AppInfo) adapter.getItem(position); shareIntent.setComponent(new ComponentName(appInfo.getAppPkgName(), appInfo.getAppLauncherClassName())); shareIntent.setType("text/plain"); // shareIntent.setType("*/*"); //這里就是組織內(nèi)容了, shareIntent.putExtra(Intent.EXTRA_TEXT, "測(cè)試,這里發(fā)送推廣地址"); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); DetailExchangeActivity.this.startActivity(shareIntent); } }); sharePopupWindow = new PopupWindow(view, (int)(160 * density), LinearLayout.LayoutParams.WRAP_CONTENT); } //使其聚焦 sharePopupWindow.setFocusable(true); //設(shè)置允許在外點(diǎn)擊消失 sharePopupWindow.setOutsideTouchable(true); // 這個(gè)是為了點(diǎn)擊“返回Back”也能使其消失,并且并不會(huì)影響你的背景 sharePopupWindow.setBackgroundDrawable(new BitmapDrawable()); //xoff,yoff基于anchor的左下角進(jìn)行偏移。正數(shù)表示下方右邊,負(fù)數(shù)表示(上方左邊) //showAsDropDown(parent, xPos, yPos); sharePopupWindow.showAsDropDown(parent, -5, 5); }
注:ShareCustomAdapter自己建一個(gè)就行了。(有一個(gè)圖標(biāo)和一個(gè)分享的名)
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android使用Intent.ACTION_SEND分享圖片和文字內(nèi)容的示例代碼
- android中Intent傳值與Bundle傳值的區(qū)別詳解
- android教程之intent的action屬性使用示例(intent發(fā)短信)
- android中intent傳遞list或者對(duì)象的方法
- Android Intent的幾種用法詳細(xì)解析
- Android 廣播大全 Intent Action 事件詳解
- 詳解Android中IntentService的使用方法
- Android中使用IntentService創(chuàng)建后臺(tái)服務(wù)實(shí)例
- Android利用Intent.ACTION_SEND進(jìn)行分享
相關(guān)文章
Android RatingBar星星評(píng)分控件實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了Android RatingBar星星評(píng)分控件,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06Android實(shí)現(xiàn)透明動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)透明動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Flutter實(shí)現(xiàn)自定義篩選框的示例代碼
本文主要介紹了Flutter實(shí)現(xiàn)自定義篩選框的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-0730條android項(xiàng)目開(kāi)發(fā)技巧與經(jīng)驗(yàn)總結(jié)
本文為大家總結(jié)了30條android項(xiàng)目開(kāi)發(fā)技巧與經(jīng)驗(yàn),,需要的朋友可以參考下2018-04-04解決ViewPager和SlidingPaneLayout的滑動(dòng)事件沖突問(wèn)題
下面小編就為大家分享一篇解決ViewPager和SlidingPaneLayout的滑動(dòng)事件沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android實(shí)現(xiàn)注冊(cè)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)注冊(cè)頁(yè)面之監(jiān)聽(tīng)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android 實(shí)現(xiàn)視頻字幕Subtitle和橫豎屏切換示例
下面小編就為大家分享一篇Android 實(shí)現(xiàn)視頻字幕Subtitle和橫豎屏切換示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤(pán)實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤(pán)實(shí)例代碼,可以應(yīng)用于Android游戲開(kāi)發(fā)中的一個(gè)應(yīng)用,需要的朋友可以參考下2014-07-07Android模擬實(shí)現(xiàn)滑動(dòng)解鎖界面
這篇文章主要為大家詳細(xì)介紹了Android模擬實(shí)現(xiàn)滑動(dòng)解鎖界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01