7

I want to publish my application (ABC). Its an audiobook file(just for example.) wrapped as apk. When the user install this application it needs to check whether another application (XYZ) already installed or not. If not let the user know they have to install the application XYZ first before installing ABC.

Thanks in advance

Rajesh

user278445
  • 271
  • 5
  • 7

2 Answers2

18

If you know the package name of the application you are looking for you can use the PackageManager to test for the existence of an application.

try{
     ApplicationInfo info = getPackageManager()
                             .getApplicationInfo("com.myproject", 0 );
     //-- application exists
    } catch( PackageManager.NameNotFoundException e ){
     //-- application doesn't exist
}
JeremyFromEarth
  • 14,344
  • 4
  • 33
  • 47
  • 1
    I don't think you can do this "on install" though. You will need to do it the first time you run the app most likely. – Mark B Mar 25 '10 at 20:14
  • right... I don't think that it is possible to run this code on install. That said, what action can you take during install process? Would it be better to check for the existence of another application the first time someone actually ran your app? – JeremyFromEarth Mar 25 '10 at 20:47
  • 7
    "That said, what action can you take during install process?" Nothing -- you do not get control under the user first launches your app. "Would it be better to check for the existence of another application the first time someone actually ran your app?" No choice -- that's the only real option. I've argued for a dependency system for APKs to deal with this situation, and the response has been...underwhelming. – CommonsWare Mar 25 '10 at 21:01
  • Thanks for chiming in CommonsWare. I like the answers that spawn questions that spawn answers. :) – JeremyFromEarth Mar 25 '10 at 21:56
2

In case XYZ is a shared library, you may set-up the android manifest of your application to prevent users installing ABC without XYZ. Please use the element inside the AndroidManifest.xml of ABC, by stating:

<uses-library android:name="package name of XYZ" android:required="true" />

Hope this help your problem.

Livio
  • 530
  • 7
  • 14
  • 1
    Does this approach inform the user that the app can't be installed as it requires a dependent app? If so does it take them to the market? – Bear Jan 02 '12 at 01:02
  • 2
    The effect of the *uses-library* setting is that the application will be filtered out by the Market, so the user will not be able to see the application *ABC* if *XYZ* is not already installed in the device: [Market Filters](http://developer.android.com/guide/appendix/market-filters.html) – Livio Jan 02 '12 at 13:56