安卓开发调起检测和调起第三方应用

检测应用是否已经安装的工具类

package com.lab.web.entity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import static android.app.Activity.RESULT_OK;

/**
 * Created by ppg on 2017/12/19.
 */

public class CheckApkExist {
    private static String facebookPkgName = "com.tencent.android.qqdownloader ";//应用宝包名

    public static boolean checkApkExist(Context context, String packageName){
        if (TextUtils.isEmpty(packageName))
            return false;
        try {
            ApplicationInfo info = context.getPackageManager()
                    .getApplicationInfo(packageName,
                            PackageManager.GET_UNINSTALLED_PACKAGES);
//            Timber.d(info.toString());
            return true;
        } catch (PackageManager.NameNotFoundException e) {
//          Timber.d(e.toString());
            return false;
        }
    }

    public static boolean checkFacebookExist(Context context){
        return checkApkExist(context, facebookPkgName);
    }

    public static boolean isAppInstalled(Context context,String packagename)
    {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
        }catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if(packageInfo ==null){
            //System.out.println("没有安装");
            return false;
        }else{
            //System.out.println("已经安装");
            return true;
        }
    }

}

未安装去市场下载,已经安装调起第三方应用

  boolean b = CheckApkExist.isAppInstalled(this, "com.tencent.android.qqdownloader");
                if (b) {//已经安装打开应用宝的劵妈妈下载页面
                    Uri uri = Uri.parse("market://details?id=" + "com.tencent.android.qqdownloader");
                    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                    try {
                       goToMarket.setClassName("com.tencent.android.qqdownloader", "com.tencent.pangu.link.LinkProxyActivity");
                        myContext.startActivity(goToMarket);
                    } catch (ActivityNotFoundException e) {
                        e.printStackTrace();
                    }
//                    Toast.makeText(myContext, String.valueOf(b), Toast.LENGTH_SHORT).show();
                }else {//未安装应用宝去下载
                    Uri uri = Uri.parse("market://details?id=com.tencent.android.qqdownloader");//id为包名 
                    Intent it = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(it);

                }

你可能感兴趣的:(Exception,第三方应用,检测调起下载)