Android 自定義分享列表ACTION_SEND
看到最近都在做自定義的東西,因為比較靈活,還可以擺脫系統(tǒng)自身不怎么漂亮的UI,(大家都懂得)所以自己也做了下自定義的分享列表,用PopupWindow的方式彈出。
1、布局:
popup_share.xml?
2、查詢手機內(nèi)所有支持分享的應(yīng)用列表 ?
public?ListgetShareApps(Context?context)?{
ListmApps?=?new?ArrayList();
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是從一個特定的應(yīng)用得到的信息。這些信息是從相對應(yīng)的Androdimanifest.xml的< application>標簽中收集到的。 ResolveInfo這個類是通過解析一個與IntentFilter相對應(yīng)的intent得到的信息。它部分地對應(yīng)于從AndroidManifest.xml的< intent>標簽收集到的信息。 得到List列表,我自建的AppInfo類,自己建一個就行 ?
private?ListgetShareAppList()?{
ListshareAppInfos?=?new?ArrayList();
PackageManager?packageManager?=?getPackageManager();
ListresolveInfos?=?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的實現(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);
ListshareAppInfos?=?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,?"測試,這里發(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è)置允許在外點擊消失
sharePopupWindow.setOutsideTouchable(true);
//?這個是為了點擊“返回Back”也能使其消失,并且并不會影響你的背景
sharePopupWindow.setBackgroundDrawable(new?BitmapDrawable());
//xoff,yoff基于anchor的左下角進行偏移。正數(shù)表示下方右邊,負數(shù)表示(上方左邊)
//showAsDropDown(parent,?xPos,?yPos);
sharePopupWindow.showAsDropDown(parent,?-5,?5);
}注:ShareCustomAdapter自己建一個就行了。(有一個圖標和一個分享的名)





