Skip to content

Instantly share code, notes, and snippets.

@sergibc
Last active March 14, 2016 14:55
Show Gist options
  • Save sergibc/834b5f6d93a0478a9baa to your computer and use it in GitHub Desktop.
Save sergibc/834b5f6d93a0478a9baa to your computer and use it in GitHub Desktop.
Class to open an installed application or launch the Play Store to download it.
import com.tempos21.android.commons.utils.T21Log;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
public class OpenAppUtils {
/**
* Open an application
*
* @param context Context
* @param applicationId Application Id we want open
*/
public static void openApplication(Context context, String applicationId) {
Intent launchIntent = context.getPackageManager()
.getLaunchIntentForPackage(applicationId);
context.startActivity(launchIntent);
}
/**
* Check if an application is installed
*
* @param context Context
* @param applicationId Application Id we want check
*/
public static boolean isApplicationInstalled(Context context, String applicationId) {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = null;
try {
applicationInfo = packageManager.getApplicationInfo(applicationId, 0);
} catch (PackageManager.NameNotFoundException e) {
T21Log.e(OpenAppUtils.class.getSimpleName(), e);
}
return applicationInfo != null;
}
/**
* Open Google Play application or the web site
*
* @param context Context
* @param applicationId Application Id we want open
*/
public static void openGooglePlay(Context context, String applicationId) {
try {
context.startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + applicationId)));
} catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + applicationId)));
} catch (Exception e) {
T21Log.e(OpenAppUtils.class.getSimpleName(), e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment