转于:http://my.oschina.net/u/861587/blog/105605
-------------------------------------------------------------------------------------------
1、布局:
popup_share.xml
01 | <?xml version="1.0" encoding="utf-8"?> |
02 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
03 | android:layout_width="fill_parent" |
04 | android:layout_height="wrap_content" > |
05 | <ListView |
06 | android:id="@+id/share_list" |
07 | android:background="#2F4F4F" |
08 | android:fadingEdge="none" |
09 | android:layout_width="fill_parent" |
10 | android:layout_height="wrap_content" |
11 | android:cacheColorHint="#00000000" |
12 | android:divider="#E2DD75" |
13 | android:dividerHeight="1.0dip" |
14 | android:headerDividersEnabled="true" |
15 | android:footerDividersEnabled="false" /> |
16 | </LinearLayout> |
popup_share_item.xml
01 | <?xml version="1.0" encoding="utf-8"?> |
02 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
03 | android:gravity="center_vertical" |
04 | android:layout_width="wrap_content" |
05 | android:layout_height="wrap_content" |
06 | android:padding="2.0dip" > |
07 | <ImageView |
08 | android:id="@+id/share_item_icon" |
09 | android:layout_width="32.0dip" |
10 | android:layout_height="32.0dip" |
11 | android:layout_marginLeft="3.0dip" |
12 | android:scaleType="fitXY" /> |
13 | <TextView |
14 | android:id="@+id/share_item_name" |
15 | android:gravity="center" |
16 | android:layout_width="wrap_content" |
17 | android:layout_height="wrap_content" |
18 | android:text="分享" |
19 | android:textColor="@color/white" |
20 | android:singleLine="true" |
21 | android:textSize="@dimen/s_size" |
22 | android:layout_marginLeft="3.0dip" |
23 | android:layout_marginRight="3.0dip" /> |
24 | </LinearLayout> |
2、查询手机内所有支持分享的应用列表
01 | public List<ResolveInfo> getShareApps(Context context) { |
02 | List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); |
03 | Intent intent = new Intent(Intent.ACTION_SEND, null); |
04 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
05 | intent.setType("text/plain"); |
06 | // intent.setType("*/*"); |
07 | PackageManager pManager = context.getPackageManager(); |
08 | mApps = pManager.queryIntentActivities(intent, |
09 | PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); |
10 | return mApps; |
11 | } |
注:ApplicationInfo是从一个特定的应用得到的信息。这些信息是从相对应的Androdimanifest.xml的< application>标签中收集到的。
ResolveInfo这个类是通过解析一个与IntentFilter相对应的intent得到的信息。它部分地对应于从AndroidManifest.xml的< intent>标签收集到的信息。
得到List列表,我自建的AppInfo类,自己建一个就行
01 | private List<AppInfo> getShareAppList() { |
02 | List<AppInfo> shareAppInfos = new ArrayList<AppInfo>(); |
03 | PackageManager packageManager = getPackageManager(); |
04 | List<ResolveInfo> resolveInfos = getShareApps(mContext); |
05 | if (null == resolveInfos) { |
06 | return null; |
07 | } else { |
08 | for (ResolveInfo resolveInfo : resolveInfos) { |
09 | AppInfo appInfo = new AppInfo(); |
10 | appInfo.setAppPkgName(resolveInfo.activityInfo.packageName); |
11 | // showLog_I(TAG, "pkg>" + resolveInfo.activityInfo.packageName + ";name>" + resolveInfo.activityInfo.name); |
12 | appInfo.setAppLauncherClassName(resolveInfo.activityInfo.name); |
13 | appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString()); |
14 | appInfo.setAppIcon(resolveInfo.loadIcon(packageManager)); |
15 | shareAppInfos.add(appInfo); |
16 | } |
17 | } |
18 | return shareAppInfos; |
19 | } |
3、弹出PopupWindow的实现
01 | private void initSharePopupWindow(View parent) { |
02 | PopupWindow sharePopupWindow = null; |
03 | View view = null; |
04 | ListView shareList = null; |
05 | if(null == sharePopupWindow) { |
06 | //加载布局文件 |
07 | view = LayoutInflater.from(DetailExchangeActivity.this).inflate(R.layout.popup_share, null); |
08 | shareList = (ListView) view.findViewById(R.id.share_list); |
09 | List<AppInfo> shareAppInfos = getShareAppList(); |
10 | final ShareCustomAdapter adapter = new ShareCustomAdapter(mContext, shareAppInfos); |
11 | shareList.setAdapter(adapter); |
12 | |
13 | shareList.setOnItemClickListener(new OnItemClickListener() { |
14 | |
15 | @Override |
16 | public void onItemClick(AdapterView<?> parent, View view, |
17 | int position, long id) { |
18 | // TODO Auto-generated method stub |
19 | Intent shareIntent = new Intent(Intent.ACTION_SEND); |
20 | AppInfo appInfo = (AppInfo) adapter.getItem(position); |
21 | shareIntent.setComponent(new ComponentName(appInfo.getAppPkgName(), appInfo.getAppLauncherClassName())); |
22 | shareIntent.setType("text/plain"); |
23 | // shareIntent.setType("*/*"); |
24 | //这里就是组织内容了, |
25 | shareIntent.putExtra(Intent.EXTRA_TEXT, "测试,这里发送推广地址"); |
26 | shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
27 | DetailExchangeActivity.this.startActivity(shareIntent); |
28 | } |
29 | }); |
30 | |
31 | sharePopupWindow = new PopupWindow(view, |
32 | (int)(160 * density), LinearLayout.LayoutParams.WRAP_CONTENT); |
33 | } |
34 | //使其聚焦 |
35 | sharePopupWindow.setFocusable(true); |
36 | //设置允许在外点击消失 |
37 | sharePopupWindow.setOutsideTouchable(true); |
38 | // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 |
39 | sharePopupWindow.setBackgroundDrawable(new BitmapDrawable()); |
40 | //xoff,yoff基于anchor的左下角进行偏移。正数表示下方右边,负数表示(上方左边) |
41 | //showAsDropDown(parent, xPos, yPos); |
42 | sharePopupWindow.showAsDropDown(parent, -5, 5); |
43 | } |
本文介绍如何获取Android设备上支持分享功能的应用列表,并展示了使用PopupWindow展示这些应用的具体实现方式。

2549

被折叠的 条评论
为什么被折叠?



