0

I am trying to open my app in

settings-> applications-> user apps-> MyApp1

i am using this snippet, it works fine, however, it fails at handling exceptions. Like when app isn't installed, in that case it should show a toast instead of shutting the whole app!

public void vpndragonsettings(View view) {
    packageName = "org.wagtailvpn.android";
    try {
        // Open the specific App Info page:
        Intent intent = new Intent(
                android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + packageName));
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast toast = Toast.makeText(this, "app isn't installed!",
                Toast.LENGTH_SHORT);
        toast.show();
    }
}
abbie
  • 345
  • 5
  • 22

1 Answers1

0

If you want to check if an app is already installed you should do this before calling the intent. I use the method below:

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

Copied from : How to check if app is installed on your phone?

Community
  • 1
  • 1
Doppie
  • 95
  • 1
  • 8