简体   繁体   中英

Android - Using try and catch

I want my try to start an intent that uses an another application. But if that application is not installed on that phone i want to show a dialog to inform the user to install the app. I tried the code below:

try {
    startActivity(i);
} catch (Exception e) {
    AlertDialog.Builder b = new AlertDialog.Builder(getApplicationContext());
    b.setMessage("Message Here");
    b.create().show();
}

`

Check installed apps by using this methond

public static boolean isAppInstalled(Context context, String packageName) {
    boolean result;
    try {
        context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        result = true;
    } catch (PackageManager.NameNotFoundException e) {
        result = false;
    }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM