Search whether android has installed an APP and search for androidapp

Source: Internet
Author: User

Search whether android has installed an APP and search for androidapp

Today, we have installed xxx.apk on the mobile phone. Because foreign projects must use gGoogle map when opening a map. For third-party software like this, android uses Intent to redirect, and then determines the type based on ACTION. An option box is displayed. However, the customer must specify a Google map. Therefore, it is necessary to filter the object during the jump. In fact, the object is filtered Based on the package name of the APK. Google map is used as an example ~ Google map's package name is com. google. android. apps. maps. If you want to know anything else, just go to Baidu. It is worth noting that if there is no Google map on the phone, the program will crash. So we have to avoid this error and throw a 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;        }    }

Add a code to retrieve all the APK files:

Final PackageManager packageManager = getActivity (). getPackageManager (); // obtain packagemanager List <PackageInfo> pinfo = packageManager. getInstalledPackages (0); // obtain the package information of all installed programs if (pinfo! = Null) {for (int I = 0; I <pinfo. size (); I ++) {String packName = pinfo. get (I ). packageName; Log. d ("info", "-->" + packName );}}

When I wrote the article, I thought about whether to check whether it was on the SD card. So I found a piece of code on the Internet:

private void isInstallOnSd(Context context,String name){      PackageManager pm=context.getPackageManager();      ApplicationInfo appInfo;      try {          appInfo = pm.getApplicationInfo(name, 0);          if ((appInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {                 // App on sdcard              Log.d("info", "app on sd");          }      } catch (NameNotFoundException e) {          e.printStackTrace();      }   }  

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.