How to make the PlayStore open if an app is not installed?

1

I'm trying to get the code to detect if an app is installed on the phone and if it is open, if it is not then open the PlayStore or rather search for the app to that is downloaded.

public void open8 (View view){

    if(InstaladaAplicacion("com.example.speedometer", getApplicationContext())){

    }else{
        Uri uri = Uri.parse("http://play.google.com/");
        Intent i = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(i);

    }

    String PackageName = "com.example.speedometer";
    Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.speedometer");

    if(intent == null) {

    }
    startActivity(intent);

}

private boolean InstaladaAplicacion(String s, Context applicationContext) {
    return false;
}

I tried this to see if it works and I think if it detects that the app is installed but I'm not sure, then having it installed opens it, I deleted the app to test if it opens the PlayStore and it turns out not to (The app it stops).

    
asked by Alejandro Matos 19.09.2017 в 00:21
source

2 answers

6

The method to detect if the application is installed on the device from its packagename , it would be:

public static boolean instaladaAplicacion(String packagename, Context context) {
    boolean response = false;
    try {
        context.getPackageManager().getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        response = true;
    } catch (PackageManager.NameNotFoundException e) {
        Log.i("Android", "NNFE aplicación no instalada: " + e.getMessage());
    }
    return response;
}

Therefore, you first search if the application defined by your packagename is installed . If it is installed, open it in the device.

If it is not installed open the application in Google Playstore for the user to install it, in this case you need to form the url with the protocol market:// .

This would be the code:

    String packageName = "com.example.speedometer";
    String MARKET_SQUEME = "market://details?id=";

    //Verifica si la aplicación se encuentra instalada.
    if(instaladaAplicacion(packageName, getApplicationContext())){
        Intent i = getPackageManager().getLaunchIntentForPackage(packageName);
        i.addCategory(Intent.CATEGORY_LAUNCHER);            
        //Abre aplicación.
        startActivity(i); 
    }else{ //No se encuentra instalada.
        //Abre aplicación en  Playstore
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(MARKET_SQUEME + packageName)));
    }
    
answered by 19.09.2017 в 01:42
1

As a complement to @Jorgesys' response

I found that not always the links market://details?id=: open the Google play app correctly, for example in my Moto G it works, but in my Acer Liquid z200 does not work.

I usually use more https://play.google.com/store/apps/details?id= than if the google play app is installed, it is shown to open it with it, but with the browser.

If you want to contemplate different scenarios if the user has Google Play installed or alternatively open the browser.

private String getGooglePlayStoreUrl(){
    String id = activity.getApplicationInfo().packageName; // current google play is   using package name as id

    PackageManager packageManager = activity.getApplicationContext().getPackageManager();
    Uri marketUri = Uri.parse("market://details?id=" + id);
    Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
    if (marketIntent.resolveActivity(packageManager) != null)
      return "market://details?id=" + id;
    else
      return "https://play.google.com/store/apps/details?id=" + id;
 }
    
answered by 19.09.2017 в 10:12