Top Ad unit 728 × 90

How to check if App is already installed in Android

We are working on our android app and we need to check whether our vLemonn.com app is installed or not. If installed we need to launch it else we need to open google play store so user can download it.

Before starting the actual coding you can check the demo what we are going to achieve in this article.



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.

<?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>

Below is the code for our MainActivity.java file

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'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 and it's time to run our app.

App is Started First Time



We click on Button to check the status.



Now we are going to open play store to download vLemonn.com App

vLemonn.com Android app is installed

Once our app is installed we are going to re check the installation status.


App Download URL from Play Store: https://play.google.com/store/apps/details?id=com.vlemonn.orion
How to check if App is already installed in Android Reviewed by Mayank Sanghvi on October 31, 2017 Rating: 5

No comments:

All Rights Reserved by Android Tutorial Fun © 2014 - 2015
Powered By Blogger, Designed by Mayank Sanghvi

Contact Form

Name

Email *

Message *

Powered by Blogger.