dependencies – How to make an Android app that depends on another app?-ThrowExceptions

Exception or error:

If I create an app that depends on another app or apps (eg: the Facebook and Twitter apps), yet they are not installed, is there a method of checking for those dependencies and installing them at the same time as my own app?

How to solve:

I did this in my application which requires the zxing scanner app to be installed.
You will want this inside your onclick or ontouch:

try{
    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    intent.setPackage("com.google.zxing.client.android");
    startActivityForResult(intent, 0);
} catch (Exception e) {
    createAlert("Barcode Scanner not installed!", "This application uses " +
    "the open source barcode scanner by ZXing Team, you need to install " +
    "this before you can use this software!", true);
}

which calls

public void createAlert(String title, String message, Boolean button) {
    // http://androidideasblog.blogspot.com/2010/02/how-to-add-messagebox-in-android.html
    AlertDialog alertDialog;
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    if ((button == true)) {
        alertDialog.setButton("Download Now",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Intent browserIntent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("market://search?q=pname:com.google.zxing.client.android"));
                startActivity(browserIntent);
            }
        });
    }
    alertDialog.show();
}

Then after sorting out all that code out I realise you asked for it to be installed at the same time as your app. Not sure if i should post this code, but it may be helpful

###

Short answer: No, you cannot automatically install other applications as dependencies.

Longer answer:

Android Market does not let you declare other applications to install as a dependency. As a system, Market appears to be designed for single application installs — not Linux distro style mega dependency graphs.

At runtime, you can test for installed apps and punt your user over to the Market if so. See the techniques suggested by @QuickNick (testing if an app is installed) and @TerryProbert (punting to market) if that’s what you want.

Your best bet is probably to design your app to gracefully degrade if dependencies are not available, and suggest (or insist) that they head over to market to install them.

###

Start from this:

Intent mediaIntent = new Intent("com.example.intent.action.NAME");
// add needed categories
List<ResolveInfo> listResolveInfo = getPackageManager().queryIntentServices(mediaIntent, 0);
if (listResolveInfo.size() != 0) {
  //normal behavior
} else {
  //install what you need
}

I give you example of querying services. If you want to check activities, then you will call queryIntentActivities().

###

I think following the pattern outlined in this post on the Android Developer Blog will help you.
http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html
As TerryProbert points out if you know that the Intent is not available prompt the user to install the missing app.

###

Here’s what I use to return the first mission activity that exists:

                try {
                Class<?> missionClass = Class.forName(mPackageName+".Mission"+mission);
                        Method missionDescription;
                        missionDescription = missionClass.getMethod("missionDescription");
                        mMissionDescription = (String) missionDescription.invoke(null);
                        if (mMissionDescription.length() > 0) {
                            nextMission = mission;
                            break;
                        }
                    } catch (Exception e) {
                        //DEBUG*/Log.v(this.getClass().getName(), "onResume: Mission no "+mission+" not found: "+e.getMessage());
                    }

Each mission is held in a separate class, derived from a Mission base class. Derived classes are called Mission1, Mission24 etc.

Not all missions are defined.

The base class has an abstract class missionDescription which returns a string describing the mission.

This code is inside a loop so tests mission=1 to 99, trying to call missionDescription. It returns when the Description for the first mission found is returned.

Leave a Reply

Your email address will not be published. Required fields are marked *