Joy Dey
Joy Dey

Reputation: 37

How to remove ads if a specific app is installed?

I have decided to remove ads for those users who have a specific app installed on their phone. Say I have an app with package name com.android.samplead and another app named com.android.removead. If the app with package name com.android.removead is installed on the phone, the app com.android.samplead shouldn't show any ads. How can this be achieved using java?

Upvotes: 1

Views: 72

Answers (1)

advice
advice

Reputation: 5998

You can easily check if an app is installed with the PackageManager.

First, you can create a method like this.

private boolean isPackageInstalled(String packageName, PackageManager packageManager) {

    boolean found = true;

    try {

        packageManager.getPackageInfo(packageName, 0);
    } catch (PackageManager.NameNotFoundException e) {

        found = false;
    }

    return found;
}

And then you can use it when you need to check it like this.

public void someMethod() {
    // ...

    PackageManager pm = context.getPackageManager();
    boolean isInstalled = isPackageInstalled("com.android.removead", pm);

    // ...
}

Taken from: https://stackoverflow.com/a/18752247/3106174

Upvotes: 2

Related Questions