公司项目需求 更新版本的时候 要指定跳入 应用宝 华为市场 本人总结如下
1.工具类 UrlHelper
package jie.android.weblearning.utils;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.List;
import jie.android.weblearning.data.APPMarketIcon;
/**
* Created by Jie on 10/19/15.
*/
public class UrlHelper {
private final static String TENCENT_PACKGET = "com.tencent.android.qqdownloader";
public static void browserOpenUrl(final Context context, final String url) {
String uri = url;
if (!uri.startsWith("http://") && !url.startsWith("https://")) {
uri = "http://" + uri;
}
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
}
//跳转到应用宝市场主流手机市场
public static void goToMarket(Context context, String packageName) {
Uri uri = Uri.parse("market://details?id=" + packageName);
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
if (getDeviceBrand().equalsIgnoreCase("XIAOMI")){
goToMarket.setClassName("com.tencent.android.qqdownloader", "com.tencent.pangu.link.LinkProxyActivity");
}
context.startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
/**
* 跳转到应用市场app详情界面
*
* @param appPkg App的包名
* @param marketPkg 应用市场包名
*/
public static void launchAppDetail(Context context, String appPkg, String marketPkg) {
try {
if (TextUtils.isEmpty(appPkg))
return;
Uri uri = Uri.parse("market://details?id=" + appPkg);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (!TextUtils.isEmpty(marketPkg))
intent.setPackage(marketPkg);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 判断应用是否安装
**/
public static boolean isAppInstalled(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
List<String> pName = new ArrayList<String>();
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
pName.add(pn);
}
}
return pName.contains(packageName);
}
/**
* 获取手机厂商
*
* @return 手机厂商
*/
public static String getDeviceBrand() {
return android.os.Build.BRAND;
}
/**
* 获取手机安装的手机市场包名
*/
public static List<APPMarketIcon> getAllMarket(Context context) {
PackageManager pManager = context.getPackageManager();
List<APPMarketIcon> packageList = new ArrayList<>();
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.APP_MARKET");
PackageManager pm = context.getPackageManager();
List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
int size = infos.size();
for (int i = 0; i < size; i++) {
APPMarketIcon appMarketIcon=new APPMarketIcon();
ActivityInfo activityInfo = infos.get(i).activityInfo;
// appMarketIcon.setPackagename(activityInfo.packageName);
appMarketIcon.setPackagename(TENCENT_PACKGET);
appMarketIcon.setAppicon(activityInfo.loadIcon(pManager));
appMarketIcon.setAppname(activityInfo.loadLabel(pManager).toString());
packageList.add(appMarketIcon);
//获取应用市场的包名
}
return packageList;
}
/**
* 跳转到对应app市场的详情页面
*/
public static void goTomarket(Context context,final String apppackagename,final String marketname) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("market://details?id=" + apppackagename);//app包名
intent.setData(uri);
intent.setPackage(marketname);//应用市场包名
context.startActivity(intent);
}
/**
* 找出手机当前安装的所有浏览器程序
*/
public static List<APPMarketIcon> getALLbrower(Context context) {
PackageManager pManager = context.getPackageManager();
List<APPMarketIcon> allbrower = new ArrayList<>();
System.out.println("******");
Uri uri = Uri.parse("http:www.baidu.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
// 通过查询,获得所有ResolveInfo对象.
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(it, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resolveInfos) {
APPMarketIcon marketIcon=new APPMarketIcon();
marketIcon.setPackagename(resolveInfo.activityInfo.packageName);
marketIcon.setAppicon(resolveInfo.loadIcon(pManager));
marketIcon.setAppname(resolveInfo.loadLabel(pManager).toString());
allbrower.add(marketIcon);
}
return allbrower;
}
}
使用 本人用的笨方法 自定义图标还是。。。 (需求要求:是华为手机 要调用出来华为应用市场 ,并且判断手机是否下载了腾讯应用宝,安装的话 华为应用市场+应用宝,非华为手机的话 只判断是否有应用宝 有的话 显示应用宝 没有的话 直接跳入网页版应用宝的应用详情)
一下是点击 立即更新 代码 片段
// List<APPMarketIcon> allMarketList = UrlHelper.getAllMarket(MainActivity.this);//所有的app市场
// List<APPMarketIcon> allbrowerList = UrlHelper.getALLbrower(MainActivity.this);//所有浏览器
List<APPMarketIcon> allMarketList=new ArrayList<>();
List<APPMarketIcon> huaweiMarketList=new ArrayList<APPMarketIcon>();
APPMarketIcon appMarketIconHuawei = new APPMarketIcon();
appMarketIconHuawei.setAppname("华为应用市场");
Drawable drawable = getResources().getDrawable(R.drawable.icon_huawei);
appMarketIconHuawei.setAppicon(drawable);
appMarketIconHuawei.setPackagename(HUAWEI_PACKET);
huaweiMarketList.add(appMarketIconHuawei);
List<APPMarketIcon> tentenctMarketList=new ArrayList<APPMarketIcon>();
APPMarketIcon appMarketIconTencent = new APPMarketIcon();
appMarketIconTencent.setAppname("应用宝");
Drawable drawable_tencent = getResources().getDrawable(R.drawable.icon_tencent);
appMarketIconTencent.setAppicon(drawable_tencent);
appMarketIconTencent.setPackagename(TENCENT_PACKGET);
tentenctMarketList.add(appMarketIconTencent);
// allMarketList.addAll(0,allbrowerList);
if (UrlHelper.getDeviceBrand().equalsIgnoreCase("HUAWEI")){
allMarketList.addAll(huaweiMarketList);
}
if (UrlHelper.isAppInstalled(MainActivity.this, TENCENT_PACKGET)) {
allMarketList.addAll(tentenctMarketList);
// UrlHelper.launchAppDetail(MainActivity.this, APP_PACKAGE, HUAWEI_PACKET);
}
if (UrlHelper.isAppInstalled(MainActivity.this, TENCENT_PACKGET)||UrlHelper.getDeviceBrand().equalsIgnoreCase("HUAWEI")){
showDialog(allMarketList);
}else {
//直接跳入网页版应用宝的应用详情
UrlHelper.browserOpenUrl(MainActivity.this, resp.getUpdateUrl());
finish();
}
public void showDialog(List<APPMarketIcon> appMarketIconList) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.dialog_app_market, null);//获取自定义布局
builder.setView(layout);
builder.setIcon(R.mipmap.icon);//设置标题图标
builder.setTitle(R.string.dialog_market_toast);//设置标题内容
//builder.setMessage("");//显示自定义布局内容
GridView gridView = (GridView) layout.findViewById(R.id.gridview_app_market);
APPMarketAdapter appMarketAdapter = new APPMarketAdapter(MainActivity.this, appMarketIconList);
gridView.setAdapter(appMarketAdapter);
//
// builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface arg0, int arg1) {
// // TODO Auto-generated method stub
// Toast.makeText(getApplication(), "ok", Toast.LENGTH_SHORT).show();
// }
// });
//取消
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
handler.obtainMessage(MSG_CREATE_OVER).sendToTarget();
}
});
final AlertDialog dlg = builder.create();
dlg.show();
}
package jane.android.weblearning.adapter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import jie.android.weblearning.R;
import jie.android.weblearning.data.APPMarketIcon;
import jie.android.weblearning.utils.UrlHelper;
/**
* Created by Jane on 2017/9/29.
*/
public class APPMarketAdapter extends BaseAdapter {
private Context context;
private List<APPMarketIcon> marketIconList;
private LayoutInflater inflater;
private final String APP_PACKAGE = "jie.android.weblearning";
private final String TENCENT_PACKGET = "com.tencent.android.qqdownloader";
public APPMarketAdapter(Context context, List<APPMarketIcon> marketIconList) {
this.context = context;
this.marketIconList = marketIconList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return marketIconList.size();
}
@Override
public Object getItem(int position) {
return marketIconList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MarketHolder marketHolder = null;
final APPMarketIcon appMarketIcon = marketIconList.get(position);
if (convertView == null) {
marketHolder = new MarketHolder();
convertView = inflater.inflate(R.layout.list_item_app_market, null);
marketHolder.imageIcon = (ImageView) convertView.findViewById(R.id.app_market_icon);
marketHolder.tv_appname = (TextView) convertView.findViewById(R.id.tv_market_appname);
convertView.setTag(marketHolder);
} else {
marketHolder = (MarketHolder) convertView.getTag();
}
marketHolder.imageIcon.setImageDrawable(appMarketIcon.getAppicon());
marketHolder.tv_appname.setText(appMarketIcon.getAppname());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String packagename = appMarketIcon.getPackagename();
if (packagename.equals("com.android.browser")) {//android 系统浏览器
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("http://a.sm.cn/details/info?ua=android&vid=21&pkg=jie.android.weblearning&ch=SM_6&uc_param_str=dn&from=wh10344");
intent.setData(content_url);
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
context.startActivity(intent);
}
UrlHelper.launchAppDetail(context, APP_PACKAGE, appMarketIcon.getPackagename());
}
});
return convertView;
}
private class MarketHolder {
private ImageView imageIcon;
private TextView tv_appname;
}
}
list_item_app_market.xml 布局文件(图片就不提供啦)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="@+id/app_market_icon"
android:layout_width="55dp"
android:layout_height="55dp"
android:src="@mipmap/icon"/>
<TextView
android:id="@+id/tv_market_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="app名称"
android:textColor="@color/black"
android:layout_margin="5dp"
android:textSize="16sp"
/>
</LinearLayout>