18

I would like to make android button and able to launch other application if already installed and go to android market if not yet installed.

How to do this?

Regards, Virak

SopheakVirak
  • 941
  • 5
  • 13
  • 35
  • 1
    Look at http://stackoverflow.com/questions/9480045/how-to-download-adobe-reader-programatically-if-not-exists/9480211#9480211 – user370305 Jul 23 '12 at 10:22

4 Answers4

35

use below code

String packageName = "app_package_name";
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);

if(intent == null) {
   intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+packageName));
}
  startActivity(intent);
Sriram
  • 6,096
  • 4
  • 18
  • 28
Vivek Kumar Srivastava
  • 2,150
  • 1
  • 16
  • 23
2

Try with this -

Just create one Button in your layout. And, onClick of that button check below condition -

Button calculateButton = (Button) findViewById(R.id.buttonCalculate);
    calculateButton.setOnClickListener(new View.OnClickListener() {

          public void onClick(View v) 
          {
              if(check() == true)
              {
                  PackageManager pack = this.getPackageManager();
                  Intent app = pack.getLaunchIntentForPackage(packagename);
                  startActivity(app);
              }else
              {
                  Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                  marketIntent.setData(Uri.parse("market://details?id=packagename"));
                  startActivity(marketIntent);
              }
         }
    });
}

public boolean check()
{
    try{
        ApplicationInfo info = getPackageManager().getApplicationInfo("packagename", 0 );
        return true;
    } catch( PackageManager.NameNotFoundException e ){
        return false;
    }
}
Praveenkumar
  • 25,322
  • 23
  • 89
  • 166
0

Try to call the Application activity from your code using the, other application package name and activity name or by the Intent filters which is belongs to that other application you need to call...

    Intent newIntent;
    newIntent = new Intent("other application Package name","class name");
    startActivity(newIntent);

Check whether it is launched or not.

//If it is launched, don't do anything

//If it isn't, then navigate the UI to Google Play Intent.

  Intent googlePlay = new Intent(Intent.ACTION_VIEW);
  googlePlay.setData(Uri.parse("market://details?id="+"other application package name"));
  startActivity(googlePlay);
Matthew Rathbone
  • 7,784
  • 7
  • 47
  • 74
Kartihkraj Duraisamy
  • 2,871
  • 2
  • 22
  • 36
0

inside onclick

@Override
public void onClick(View view){
  try{
    startActivity(getPackageManager().getLaunchIntentForPackage("applicationId"));
  } catch (PackageManager.NameNotFoundException e) {
    startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://play.google.com/store/apps/details?id=" + "applicationId")));
 }
}
mayank1513
  • 4,971
  • 3
  • 23
  • 70