To determine how the phone has installed a program:
Private Booleanisavilible (Context context, String PackageName) {FinalPackagemanager Packagemanager = Context.getpackagemanager ();//Get Packagemanagerlist< packageinfo> pinfo = packagemanager.getinstalledpackages (0);//Get package information for all installed programsList<string> PName =NewArraylist<string> ();//the package name used to store all installed programs//Remove the package name from pinfo and press it into the PName list if(pinfo! =NULL){ for(inti = 0; I < pinfo.size (); i++) {String pn=Pinfo.get (i). PackageName; Pname.add (PN); } } returnPname.contains (PackageName);//determine if there is a package name for the target program in PName, there is true, no false}
The logic after judgment:
//installed, open the program, need to pass in the parameter package name: "COM.TENCENT.MM"if(Isavilible ( This, "Com.skype.android.verizon")) {Intent I=NewIntent (); ComponentName cn=NewComponentName ("com.tencent.mm", "Com.tencent.mm.WeiXinActivity"); I.setcomponent (CN); Startactivityforresult (i, RESULT_OK); } //not installed, jump to market download the programElse{URI Uri= Uri.parse ("market://details?id=com.tencent.mm");//ID is the package name
download directly from the specified URL
// uri uri = Uri.parse ("http://dldir1.qq.com/foxmail/weixin521android400.apk");
Intent it =NewIntent (Intent.action_view, URI); StartActivity (IT); }
Android detects if an app is installed on your phone