MainActivity.java中 downloadApp() 方法用于调用
public void downloadApp(){
String packageName = "org.bojoy.publish";/** Tesr 测试包名*/
final String downloadURL = "http://www.haowanyou.com/";/** Test 测试跳转下载的页面*/
Intent intent = null;
PackageManager packageManager = context.getPackageManager();
if (AppIsInstall.appIsInstall(context, packageName)){
intent = packageManager.getLaunchIntentForPackage(packageName);
activity.startActivity(intent);
}else {
final BJMSdkDialog dialog = new BJMSdkDialog(context);// 这是自定义的dialog 测试请用系统的
dialog.setTitle(Util.getString(Resource.string.bjmgf_sdk_dock_dialog_to_download_app_title, context));
dialog.setMessage(Util.getString(Resource.string.bjmgf_sdk_dock_dialog_to_download_app_str, context));
dialog.setNegativeButton(Util.getString(Resource.string.bjmgf_sdk_dock_dialog_sure_download_btn_str,
context), new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Util.openUrl(activity, downloadURL);
}
});
dialog.setPositiveButton(Util.getString(Resource.string.bjmgf_sdk_dock_dialog_btn_cancel_str,
context), new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
Util.java 中 openUrl()方法调用系统浏览器,这是如果没有安装调用浏览器跳转到下载页面
/**
* 调用浏览器打开网页
* @param activity
* @param url
*/
public final static void openUrl(Activity activity, String url) {
if (stringIsEmpty(url)) {
return;
}
LogProxy.i(TAG, url);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri content_url = Uri.parse(url);
intent.setData(content_url);
activity.startActivity(Intent.createChooser(intent, "打开页面"));
// activity.startActivity(intent);
}
再开一个工具类Utils.java 里面的判断方法
package org.bojoy.gamefriendsdk.app.utils;
import java.util.List;
import org.bojoy.core.utils.LogProxy;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.Context;
/**
* @author luzhenyu
* AppIsInstall 判断某款应用是否安装的工具
* @param context - Context
* @param packageName - String
* @return true/false
* */
public class AppIsInstall {
private String packageName = null;
private static boolean flag = false;
public static boolean appIsInstall(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
List<PackageInfo> packageInfoList = packageManager
.getInstalledPackages(0);
if (packageName == null || "".equals(packageName)) {
return false;
}
for (int i = 0; i < packageInfoList.size(); i++) {
PackageInfo pak = (PackageInfo) packageInfoList.get(i);
if (packageName.trim().equals(pak.packageName.trim())) {
flag = true;
break;
} else {
flag = false;
}
}
return flag;
}
}
OK!!!
参考博客:http://blog.csdn.net/liguangzhenghi/article/details/8185751
【欢迎上码】
【微信公众号搜索 h2o2s2】