分享应用列表怎么在Android应用中获取?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联建站专业网站设计制作、网站建设,集网站策划、网站设计、网站制作于一体,网站seo、网站优化、网站营销、软文营销等专业人才根据搜索规律编程设计,让网站在运行后,在搜索中有好的表现,专业设计制作为您带来效益的网站!让网站建设为您创造效益。
Android获取分享应用列表
1、布局:
popup_share.xml
<?xml version="1.0" encoding="utf-8"?>
popup_share_item.xml
<?xml version="1.0" encoding="utf-8"?>
2、查询手机内所有支持分享的应用列表
public ListgetShareApps(Context context) { List mApps = 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是从一个特定的应用得到的信息。这些信息是从相对应的Androdimanifest.xml的< application>标签中收集到的。
ResolveInfo这个类是通过解析一个与IntentFilter相对应的intent得到的信息。它部分地对应于从AndroidManifest.xml的< intent>标签收集到的信息。
得到List列表,我自建的AppInfo类,自己建一个就行
private ListgetShareAppList() { List shareAppInfos = new ArrayList (); PackageManager packageManager = getPackageManager(); List 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 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, "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);
//设置允许在外点击消失
sharePopupWindow.setOutsideTouchable(true);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
sharePopupWindow.setBackgroundDrawable(new BitmapDrawable());
//xoff,yoff基于anchor的左下角进行偏移。正数表示下方右边,负数表示(上方左边)
//showAsDropDown(parent, xPos, yPos);
sharePopupWindow.showAsDropDown(parent, -5, 5);
}
注:ShareCustomAdapter自己建一个就行了。(显示会有一个图标和一个分享的名字)
看完上述内容,你们掌握分享应用列表怎么在Android应用中获取的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
Copyright © 2009-2022 www.fjjierui.cn 青羊区广皓图文设计工作室(个体工商户)达州站 版权所有 蜀ICP备19037934号