Retrieves whether Android installs an app

Source: Internet
Author: User

今天和大家分享如何判断手机上是否安装了XXX.apk。因为国外项目在打开地图的时候一定要使用gGoogle map,像这种打开第三方软件的,android都会采用Intent进行跳转,然后根据ACTION判断类型,弹出一个选项框。但是客户非要指定Google map,所以在跳转的时候就要进行筛选,其实就是根据APK的包名进行筛选以Google map 为例~Google map的包名为com.google.android.apps.maps,如果你想知道其他的,直接百度下就好了。值得注意的是,如果手机上没存在Google map,程序会崩溃。所以在做的时候得规避下这种错误,抛出一个Tomast。
if (Checkapkexist (Getactivity (),"Com.google.android.apps.maps") {Intent i = new Intent (Intent. ACTION_view, Uri. Parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q="+ (Double. Parsedouble(mlocation. Getlongitude())                                                            +","+ Double. Parsedouble(mlocation. Getlongitude()))));I. Addflags(Intent. FLAG_activity_new_task & Intent. FLAG_activity_exclude_from_recents);I. Setclassname("Com.google.android.apps.maps","Com.google.android.maps.MapsActivity");StartActivity (i);} else {Toast. Maketext(Getactivity (),"Please download Google map", Toast. LENGTH_long);}boolean checkapkexist (Context context, String PackageName) {if (PackageName = = NULL | |"". Equals(PackageName)) {return False;} try {context. Getpackagemanager(). Getapplicationinfo(PackageName, Packagemanager. GET_uninstalled_packages);return True;} catch (Namenotfoundexception e) {return false;}    }

Also attach the previous code to retrieve all APK:

final PackageManager packageManager = getActivity().getPackageManager();//获取packagemanager   List< PackageInfo> pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息       ifnull){       for(int0; i < pinfo.size(); i++){           String packName = pinfo.get(i).packageName;           Log.d("info""-->"+packName);      }   

Again when writing an article and think about whether to detect whether in the SD card, so I found a piece of code from the Internet:

privatevoidisInstallOnSd(Context context,String name){      PackageManager pm=context.getPackageManager();      ApplicationInfo appInfo;      try {          0);          if0) {                 // App on sdcard              Log.d("info""app on sd");          }      catch (NameNotFoundException e) {          e.printStackTrace();      }   }  

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Retrieves whether Android installs an app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.