Farhan
Farhan

Reputation: 3226

android: detecting specific android applications

I need a suggestion here. Consider that I have two applications Application1 and Application2. Before I launch Application2, I want to know whether Application1 exists or not? Can anyone suggest the best method of achieving this.

Thank you

Upvotes: 0

Views: 103

Answers (3)

tennessee sombrero
tennessee sombrero

Reputation: 93

Have you looked at the Android PackageManager? You can use it in Application2 to see if Application1 is installed; if it isn't you can take whatever steps you like.

Upvotes: 2

barry
barry

Reputation: 4147

Here's how I detect a target package and launch it:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Intent targetIntent = getTargetPackage();
    if(targetIntent != null)
    {
        startActivity(targetIntent);
    }
    else
    {
        Toast.makeText(this, getResources().getString(R.string.target_package_not_installed_error), Toast.LENGTH_LONG).show();
    }
    finish();
}

private Intent getTargetPackage()
{
    packageManager = getPackageManager();
    Intent targetIntent = packageManager.getLaunchIntentForPackage(TARGET_PACKAGE);
    return targetIntent;
}

Hope this helps.

Upvotes: 1

MrZander
MrZander

Reputation: 3120

I'm not quite sure of your current implementation, you didn't provide much detail, but here is what I did for an app that used the Barcode Scanner app.

try{
    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    intent.putExtra("SCAN_MODE", "ONE_D_MODE");
    startActivityForResult(intent, 0);
}catch(ActivityNotFoundException ex){
    //This means the activity was not found
}

It shouldn't be too difficult to make it work for your implementation.

Upvotes: 1

Related Questions