Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;

import android.content.pm.ResolveInfo;

public class Main {

    public static ActivityInfo getAppInfo(String appName, Context context) {
        try {
            List<ResolveInfo> appList = getAllAppInfo(context);
            if (appList != null && !appList.isEmpty() && appName != null && !"".equals(appName)) {
                for (int i = 0; i < appList.size(); i++) {
                    String appNameTemp = appList.get(i).loadLabel(context.getPackageManager()).toString();
                    // String packageName =
                    // appList.get(i).activityInfo.packageName;
                    // String mainName = appList.get(i).activityInfo.name;
                    // Log.i(TAG, "appName:" + appName + "\n" + "packageName:" +
                    // packageName + "\n" + "mainName:" + mainName);
                    if (appName.equals(appNameTemp)) {
                        return appList.get(i).activityInfo;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<ResolveInfo> getAllAppInfo(Context context) {
        try {
            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            List<ResolveInfo> appList = context.getPackageManager().queryIntentActivities(mainIntent, 0);
            return appList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}