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.