How To Know The APP You Want To Start Is Installed - Android

make every day exciting!!

1. If you just want a detector to ensure whether or not the app is existed, you can do this as below:

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

2. Or, you can do this to ensure that you can find a app to get start without error as below:

Intent activityIntent = new Intent();
.... (set the Intent for the Activity get ready to start.)

try
{
    activity.startActivity( Intent.createChooser( activityIntent , 
            activity.getString( "chooser title" ) ) );
}
catch ( android.content.ActivityNotFoundException ex )
{
    // if there is no application working, go to google play 
    // and install the any APP in play store for start next step.

    AlertDialog.Builder alertDialog =
                new AlertDialog.Builder( activity );
    alertDialog.setMessage( " APP is not existed now! " );
    alertDialog.setCancelable( true );
    alertDialog.setNegativeButton( "NO", null );
    alertDialog.setPositiveButton( "OK",  new DialogInterface.OnClickListener()
    {
        @Override
public void onClick( DialogInterface dialog, int which )
{
    GooglePlayInstaller( activity, [App Package Name in Google Play] );
}
    });
    alertDialog.show();
}

留言

這個網誌中的熱門文章

7-ELEVEN 電子發票明細查詢方式

Java 使用 regular expression 正則表達,過濾特殊字元,反斜線處理重點

Ubuntu GUI中,無法啟動WIFI連結解決方法