转于: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 publicList<ResolveInfo> getShareApps(Context context) {
02 List<ResolveInfo> mApps =newArrayList<ResolveInfo>();
03 Intent intent =newIntent(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 returnmApps;
11 }
注:ApplicationInfo是从一个特定的应用得到的信息。这些信息是从相对应的Androdimanifest.xml的< application>标签中收集到的。 ResolveInfo这个类是通过解析一个与IntentFilter相对应的intent得到的信息。它部分地对应于从AndroidManifest.xml的< intent>标签收集到的信息。 得到List列表,我自建的AppInfo类,自己建一个就行
01 privateList<AppInfo> getShareAppList() {
02 List<AppInfo> shareAppInfos =newArrayList<AppInfo>();
03 PackageManager packageManager = getPackageManager();
04 List<ResolveInfo> resolveInfos = getShareApps(mContext);
05 if(null== resolveInfos) {
06 returnnull;
07 }else{
08 for(ResolveInfo resolveInfo : resolveInfos) {
09 AppInfo appInfo =newAppInfo();
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 returnshareAppInfos;
19 }

3、弹出PopupWindow的实现

view source print ?
01 privatevoidinitSharePopupWindow(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 finalShareCustomAdapter adapter =newShareCustomAdapter(mContext, shareAppInfos);
11 shareList.setAdapter(adapter);
12
13 shareList.setOnItemClickListener(newOnItemClickListener() {
14
15 @Override
16 publicvoidonItemClick(AdapterView<?> parent, View view,
17 intposition,longid) {
18 // TODO Auto-generated method stub
19 Intent shareIntent =newIntent(Intent.ACTION_SEND);
20 AppInfo appInfo = (AppInfo) adapter.getItem(position);
21 shareIntent.setComponent(newComponentName(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 =newPopupWindow(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(newBitmapDrawable());
40 //xoff,yoff基于anchor的左下角进行偏移。正数表示下方右边,负数表示(上方左边)
41 //showAsDropDown(parent, xPos, yPos);
42 sharePopupWindow.showAsDropDown(parent, -5,5);
43 }

更多相关文章

  1. Android(安卓)获取设备各种信息
  2. android 获取手机设备信息
  3. android -- 小功能 Android为多媒体文件生成缩略图
  4. Android获取设备信息(利用反射)
  5. Android通过socket连接服务器(PC)
  6. Android(安卓)获取内存信息
  7. Android(安卓)获取播放视频的相关 内容, 分辨率, 方向,作者信息
  8. android 利用startActivityForResult()方法得到另一个Activity的
  9. android 获取手机位置信息

随机推荐

  1. Android(安卓)adb 无线调试
  2. android IOS在WebView 和 JS 交互对比
  3. 【android】listview动态更新(与数据库之
  4. Unity3D 调用Android原生方法
  5. ANDROID IOS对字体加粗
  6. android 进程内存查看
  7. Android:动画效果translate、scale、alph
  8. android Immutable bitmap passed to Can
  9. android软件安全攻防实例第一章笔记
  10. Android中两种设置全屏的方法 && Android