문제

Suppose I have a menu option called 'Display' and my application com.android.display is performing the task of displaying the picture.Now if the apk com.android.display is not installed on the device, the menu option 'display' comes but its of no use.So I want that only when this apk is installed on device then only 'display' menu option comes. Is there any way to achieve this on Android OS?

도움이 되었습니까?

해결책

This particular solution is on Android How To on Google Sites

If you ever need to know if a particular app is installed on the user's device, you can use the PackageManager. From a Context class (e.g. an Activity or a Service) you can call getPackageManager(). This gives you a variety of methods, one of which is getPackageInfo(). Below is a method you might use. You might call it like this:

isAppInstalled("com.simexusa.campusmaps_full");

private boolean isAppInstalled(String uri) {
   PackageManager pm = getPackageManager();
   boolean installed = false;
   try {
      pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
      installed = true;
   } catch (PackageManager.NameNotFoundException e) {
      installed = false;
   }
   return installed;
}

다른 팁

There are several ways to approach this.

The first, and probably best, is to display the option in all cases. If the other APK is not install, when you try to activate it, by sending it an intent, Android will throw an exception. Simply catch this exception, and offer to direct the user to the market where they can install the missing application. This way, the user is aware that they have an option for extra functionality, and can fairly easily install it.

If you insist on gong your original route, I would not adopt the solution above, as it is possible that the same functionality is offered by more than one APK. If you check for the specific APK, then the user might have another APK that handles the same intent, and you are not offering them the functionality, despite it being supported.

Instead, I would check whether the specific intent has a handler. You can find code to do this here.

Shachar

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top