http://www.eoeandroid.com/forum.php?mod=viewthread&tid=113258
获取可以使用share功能的程序
public static 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");
PackageManager pManager = context.getPackageManager();
mApps = pManager.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
return mApps;
}
打包成list
private List<ResolveInfo> resolveList;
private PackageManager pManager;
protected List<Map<String, Object>> getNoteData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
pManager = this.anchor.getContext().getPackageManager();
resolveList = getShareApps(this.anchor.getContext());
if (resolveList == null) {
return null;
} else {
int length = resolveList.size();
for (int i = 0; i < length; i++) {
ResolveInfo resolve = resolveList.get(i);
map = new HashMap<String, Object>();
// 更多可获取的信息,参考前面的文章
map.put("title", resolve.loadLabel(pManager).toString());
map.put("img", resolve.loadIcon(pManager)); // 如果使用listview,这个地方无效,需要直接在getView里面设置
list.add(map);
}
}
return list;
}
上面的那个list放入listview,单击每个item后处理:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (resolveList != null && pManager != null) {
ResolveInfo resolve = resolveList.get(arg2);
Intent it = new Intent(Intent.ACTION_SEND);
it.setPackage(resolve.activityInfo.packageName);
it.putExtra(Intent.EXTRA_TEXT, "Custom Text");
it.setType("text/plain");
anchor.getContext().startActivity(it);
}
}
});