国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現自定義分享列表ACTION_SEND功能的方法

2019-10-23 19:45:29
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現自定義分享列表ACTION_SEND功能的方法。分享給大家供大家參考,具體如下:

看到最近都在做自定義的東西,因為比較靈活,還可以擺脫系統自身不怎么漂亮的UI,(大家都懂得)所以自己也做了下自定義的分享列表,用PopupWindow的方式彈出。

先上效果圖:

Android,自定義,分享列表,ACTION_SEND

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、查詢手機內所有支持分享的應用列表

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是從一個特定的應用得到的信息。這些信息是從相對應的Androdimanifest.xml的< application>標簽中收集到的。

ResolveInfo這個類是通過解析一個與IntentFilter相對應的intent得到的信息。它部分地對應于從AndroidManifest.xml的< intent>標簽收集到的信息。

得到List列表,我自建的AppInfo類,自己建一個就行

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的實現

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("*/*");          //這里就是組織內容了,          shareIntent.putExtra(Intent.EXTRA_TEXT, "測試,這里發送推廣地址");          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);    //設置允許在外點擊消失    sharePopupWindow.setOutsideTouchable(true);    // 這個是為了點擊“返回Back”也能使其消失,并且并不會影響你的背景    sharePopupWindow.setBackgroundDrawable(new BitmapDrawable());    //xoff,yoff基于anchor的左下角進行偏移。正數表示下方右邊,負數表示(上方左邊)    //showAsDropDown(parent, xPos, yPos);    sharePopupWindow.showAsDropDown(parent, -5, 5);}

注:ShareCustomAdapter自己建一個就行了。(有一個圖標和一個分享的名)

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 周宁县| 包头市| 奉化市| 含山县| 贵阳市| 洛南县| 和硕县| 普格县| 河北区| 桐城市| 驻马店市| 容城县| 东辽县| 慈溪市| 历史| 尼玛县| 曲麻莱县| 荔波县| 吉安市| 准格尔旗| 都兰县| 双江| 临沂市| 杭锦后旗| 肇源县| 崇左市| 皮山县| 海林市| 巴林左旗| 杭锦后旗| 荣昌县| 府谷县| 陇南市| 崇阳县| 岗巴县| 贵阳市| 镇雄县| 钦州市| 汉寿县| 兰州市| 炉霍县|