Check if App is already installed in Android

Check if App is already installed in Android

In our vLemonn.com android app we need to check installation for other apps. If it is not installed we need to redirect user to google play store. Below is the demo video what we are going to achive in this article.

As shown in the above video in our activity_main.xml file we have a TextView and a Button. We are using TextView to display important messages. Button is used to check the status. Below is the source code for activity_main.xml file.

Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.vlemonn.blog.applicationinstallatoinstatus.MainActivity">
    <!--
        TextView: It is used to display message.
    -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello vlemonn!"
        android:id="@+id/tvMessage"
        android:layout_margin="10dp"
        android:textStyle="bold"
        />
    <!--
        Button: It is used to check the vLemonn.com Application Installation Status.
    -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Is vLemonn.com app installed ?"
        android:id="@+id/button"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        />
    <!--
        Button: It is used to open Play Store.
    -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Play Store"
        android:id="@+id/buttonDownload"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        />
</LinearLayout>

It’s time to code for MainActivity.java class. In our MainActivity.java class we are going to use PackageManager. It is used for retrieving various kinds of information related to application packages that are currently installed on the device. We can access this class through getPackageManager(). In PackageManager we are going touse queryIntentActivities public method to get all the installed apps. Below is the more information on queryIntentActivities method

  • Return : List<ResolveInfo>
  • Parameter 1: Intent
  • Parameter 2: int

List<ResolveInfo> queryIntentActivities (Intent intent,int flags)

In ResolveInfo we are interested in ActivityInfo class. In ApplicationInfo we want ApplicationInfo. And finally packageName from ApplicationInfo.

Below is the code for MainActivity.java file.

Code

package com.vlemonn.blog.applicationinstallatoinstatus;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button mButton,mButtonDownload;
    private TextView mTvMessage;

    // String variable to hold apps package name.
    private String mPackageName = "com.vlemonn.orion";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.button);
        mTvMessage = (TextView) findViewById(R.id.tvMessage);
        mButtonDownload = (Button) findViewById(R.id.buttonDownload);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAppInstallationStatus();
            }
        });

        mButtonDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openPlayStore();
            }
        });
    }

    private void checkAppInstallationStatus()
    {
        final List<String> installedPackages = getInstalledAppsPackage();
        if(installedPackages.contains(mPackageName)){
            mTvMessage
                    .setText("Yes, vLemonn.com App is Installed");
        }else {
            mTvMessage
                    .setText("No, vLemonn.com App is not Installed");
        }

    }

    protected List<String> getInstalledAppsPackage(){
        // Initialize a new intent
        Intent intent = new Intent(Intent.ACTION_MAIN,null);

        // Set intent category
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        // Set intent flags
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                |Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

        // Initialize a new list of resolve info
        List<ResolveInfo> resolveInfoList =
                getPackageManager().queryIntentActivities(intent,0);

        // Initialize a new list of package name
        List<String> packageNameList = new ArrayList<>();

        for(ResolveInfo resolveInfo: resolveInfoList){

            // Get the activity info from resolve info
            ActivityInfo activityInfo = resolveInfo.activityInfo;

            // Get the package name from activity info&#39;s application info
            // Add the package name to the list
            packageNameList.add(activityInfo.applicationInfo.packageName);
        }

        // Return the package name list
        return packageNameList;
    }

    private void openPlayStore() {
        try {
            startActivity(new Intent(Intent.ACTION_VIEW
                    , Uri.parse("market://details?id=" + mPackageName)));
        } catch (android.content.ActivityNotFoundException anfe) {
            startActivity(new Intent(Intent.ACTION_VIEW
                    , Uri.parse("https://play.google.com/store/apps/details?id="
                    + mPackageName)));
        }
    }
}

Our code is ready. Let’s execute our app and test it.

Below is the image for first time of execution.

Check if App is already installed in Android Launch App First Time

Once the app is running we clicked on “IS VLEMONN.COM APP INSTALLED?” button to check vLemonn.com app installation status. Below is the output after button click.

Check if App is already installed in Android: vLemonn.com app is not installed

Our vLemonn.com app is not installed. Now we are going to use second button to open play store so we can download our app and check status again. Result after click on “OPEN PLAY STORE” button.

Check if App is already installed in Android: Play store page for installing vLemonn.com app

Ok It’s time to install app. Click on Install Button.

Check if App is already installed in Android: vLemonn.com app is installed

Our app is installed successfully. Now it’s time to check app installation status again. Again launch our testing app and click on “IS VLEMONN.COM APP INSTALLED?” button.

Check if App is already installed in Android: vLemonn.com app is installed

What Next ? It’s time for Excersie for you

In our testing app we used two buttons one for checking status and another for opening play store. Now you need to use a single button to do the same.

0 Comments
Leave A Comment

Please login to post your valuable comments.

Join the newsletter

Get the latest vLemonn news first

share