This sample code comes handy when you have to check that an application is already installed in your ANDROID Phone.
Call the below function appInstalledOrNot() with the package name of the app installed on the ANDROID Device as a string parameter.

This function returns true if the app is installed otherwise returns false.

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;

public class Example extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	//Put the package name here...
    	boolean installed  =   appInstalledOrNot("com.Ch.Example.pack");
    	if(installed)
    	{
    	          System.out.println("App already installed om your phone");
    	}
    	else
    	{
    		System.out.println("App is not installed om your phone");
    	}
    }
    private boolean appInstalledOrNot(String uri)
    {
        PackageManager pm = getPackageManager();
        boolean app_installed = false;
        try
        {
               pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
               app_installed = true;
        }
        catch (PackageManager.NameNotFoundException e)
        {
               app_installed = false;
        }
        return app_installed ;
}
}
 

2 Comments

  1. parth March 25, 2017 at 2:28 pm

    Your method takes package name as a parameter. I want to know if we can use the application name instead?

     
    • James April 2, 2017 at 2:35 pm

      You cannot use application name because different application can have same name.

       

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.