how to open android application if installed and go to android market if not install

后端 未结 4 1889
滥情空心
滥情空心 2020-12-30 02:48

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?

4条回答
  •  温柔的废话
    2020-12-30 03:47

    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;
        }
    }
    

提交回复
热议问题