Android獲取分享應(yīng)用列表詳解及實(shí)例
Android獲取分享應(yīng)用列表詳解及實(shí)例
如果在應(yīng)用的AndroidManifest.xml中含有 ACTION_SEND 屬性,那就證明該應(yīng)用可以供第三方應(yīng)用進(jìn)行調(diào)用分享,那怎么獲取函數(shù)該屬性的分享列表了,這對(duì)我們做應(yīng)用的非常有用;最近在做該功能,自己也做了下自定義的分享列表,用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è)類是通過(guò)解析一個(gè)與IntentFilter相對(duì)應(yīng)的intent得到的信息。它部分地對(duì)應(yīng)于從AndroidManifest.xml的< intent>標(biāo)簽收集到的信息。
得到List列表,我自建的AppInfo類,自己建一個(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, "test");
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è)就行了。(顯示會(huì)有一個(gè)圖標(biāo)和一個(gè)分享的名字)
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
基于RxJava實(shí)現(xiàn)酷炫啟動(dòng)頁(yè)
本文介紹怎樣利用RxJava來(lái)實(shí)現(xiàn)Android的啟動(dòng)頁(yè),啟動(dòng)頁(yè)的效果非??幔行枰呐笥褌兛梢詤⒖?。2016-07-07
詳解Flutter如何使用Completer實(shí)現(xiàn)防抖功能
防抖是用于確保時(shí)間內(nèi)的所有觸發(fā)被合并成單一請(qǐng)求,在Flutter中,我們可以使用Completer 來(lái)實(shí)現(xiàn)防抖功能,下面我們就來(lái)看看具體實(shí)現(xiàn)方法吧2024-03-03
Android實(shí)現(xiàn)一個(gè)簡(jiǎn)易的帶邊輸入框
如今市面上APP的輸入框可以說(shuō)是千奇百怪,不搞點(diǎn)花樣出來(lái)貌似代表格局沒(méi)打開(kāi),還在使用系統(tǒng)自帶的輸入框的兄弟可以停下腳步,哥帶你實(shí)現(xiàn)一個(gè)簡(jiǎn)易的帶邊框輸入框,感興趣的同學(xué)可以自己動(dòng)手試一下2023-08-08
RecyclerView實(shí)現(xiàn)仿支付寶應(yīng)用管理
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)仿支付寶應(yīng)用管理的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android開(kāi)發(fā)中的Surface庫(kù)及用其制作播放器UI的例子
這篇文章主要介紹了Android開(kāi)發(fā)中的Surface庫(kù)及用其制作播放器界面的例子,利用SurfaceView和SurfaceHolder可以高效地繪制和控制圖形界面,需要的朋友可以參考下2016-04-04
Android提高之Android手機(jī)與BLE終端通信
這篇文章主要介紹了Android手機(jī)與BLE終端通信的方法,有很大的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08
Android獲取當(dāng)前運(yùn)行的類名或者方法
這篇文章主要介紹了Android獲取當(dāng)前運(yùn)行的類名或者方法,涉及Android操作類與方法的技巧,需要的朋友可以參考下2015-05-05
Android 使用FragmentTabhost代替Tabhost
這篇文章主要介紹了Android 使用FragmentTabhost代替Tabhost的相關(guān)資料,需要的朋友可以參考下2017-05-05
怎樣才能導(dǎo)入別人的android項(xiàng)目不再報(bào)錯(cuò)
每次看到好的項(xiàng)目都想拿過(guò)來(lái)看看源碼,可是導(dǎo)入以后各種報(bào)錯(cuò)怎么辦?源碼有問(wèn)題嗎?有這種可能,但更多的可能性是你沒(méi)有正確導(dǎo)入這個(gè)項(xiàng)目2021-08-08

