I need to make a transition button in the Viber application. The button is there, and the transition works like this:

Intent viber = new Intent(Intent.ACTION_VIEW, Uri.parse("viber://add?number=79101234567")); 

But I need to make sure that if viber is not installed, the button will transfer me to the market to install this application.

    2 answers 2

    Try this. A source

      public class Example extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String appPackageName = "com.viber.voip"; boolean installed = appInstalledOrNot(appPackageName); Button buttonX = (Button)findViewById(R.id.buttonXName); buttonX.setOnClickListener(new OnClickListener() { public void onClick(View v){ if(installed) { Intent viber = new Intent(Intent.ACTION_VIEW, Uri.parse("viber://add?number=79101234567")); startActivity(viber ); } else { try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); } catch (android.content.ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); } } }); } private boolean appInstalledOrNot(String uri) { PackageManager pm = getPackageManager(); try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { } return false; } } 
    • I am not an experienced android developer. How to fasten it to the button? - Artem Novikov
    • Updated the answer, try it - DevOma

    Check whether the application is installed or not:

     private boolean isInstallApp(String appName) { PackageManager packageManager = getPackageManager(); try { packageManager.getPackageInfo(appName, PackageManager.GET_ACTIVITIES); return packageManager.getApplicationInfo(appName, 0).enabled; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return false; } }