结合Brvah开源框架,获取Android手机上所有的应用和包名,存放HashMap集合中,加载产品的时候判断是否已安装产品,有就显示打开,没有就直接安装。
1.点击跳转已安装应用(MultipleItemType)
private Map<String, String> appMap;
final String apkName = appMap.get(item.title);
if (EmptyUtils.isNotEmpty(apkName)) {
helper.setText(R.id.tv_item_product_record_list_type, "打开");
} else {
helper.setText(R.id.tv_item_product_record_list_type, "安装");
}
helper.setOnClickListener(R.id.rl_product_record_list, new View.OnClickListener() {
@Override
public void onClick(View v) {
if (EmptyUtils.isNotEmpty(apkName)) {
AppUtils.launchApp(apkName);//
} else {
//...
}
}
});
2.遍历所哟应用和包名 放在HashMap集合里
private void loadApps() {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(intent, 0);
appMap = new HashMap<String, String>();
for (int i = 0; i < apps.size(); i++) {
ResolveInfo info = apps.get(i);
String name = (String) info.activityInfo.loadLabel(context.getPackageManager());
String packageName = info.activityInfo.packageName;
appMap.put(name, packageName);
}
}
3.打开应用
private void launchApp(String packageName) {
Intent intent = getContext().getPackageManager().getLaunchIntentForPackage(packageName);
startActivity(intent);
}