Wednesday, October 4, 2017

android - Check if app installed

activity_main.xml

<?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"
    android:id="@+id/base_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#c6d4ad"
    android:padding="16dp"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textStyle="bold"
        />
    <Button
        android:id="@+id/btn_do_task"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check If App Installed"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

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

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


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private LinearLayout mBaseLayout;
    private Button mBtnDoTask;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widgets reference from xml layout
        mBaseLayout = findViewById(R.id.base_layout);
        mBtnDoTask = findViewById(R.id.btn_do_task);
        mTextView = findViewById(R.id.text_view);

        // Get the installed apps package name list
        final List<String> installedPackages = getInstalledAppsPackageNameList();

        // Set a click listener for button widget
        mBtnDoTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // String variables to hold apps package name
                String packageNameFacebook = "com.facebook.katana";
                String packageNameFacebookLite = "com.facebook.lite";

                // Check facebook is installed in the device or not
                if(installedPackages.contains(packageNameFacebook)){
                    mTextView.setText("Facebook is installed");
                }else {
                    mTextView.setText("Facebook is not installed");
                }

                // Check facebook lite is installed in the device or not
                if(installedPackages.contains(packageNameFacebookLite)){
                    mTextView.append("\n\nFacebook Lite is installed");
                }else {
                    mTextView.append("\n\nFacebook Lite is not installed");
                }
            }
        });
    }

    // Custom method to get all installed apps package name list
    protected List<String> getInstalledAppsPackageNameList(){
        // 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;
    }
}