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?
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);
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")));
}
}
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;
}
}
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);