How To Know The APP You Want To Start Is Installed - Android
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();
}
留言
張貼留言