Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.app.ActivityManager;
import android.content.Context;

import android.text.TextUtils;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.List;

public class Main {
    private static final String TAG = "Freeline.AppUtils";

    public static boolean isMainProcess(Context context) {
        String packageName = context.getPackageName();
        String processName = getCurProcessName(context);
        return packageName.equalsIgnoreCase(processName);
    }

    public static String getCurProcessName(Context context) {
        String strRet = null;
        try {
            Class<?> clazz = Class.forName("android.ddm.DdmHandleAppName");
            Method method = clazz.getDeclaredMethod("getAppName");
            strRet = (String) method.invoke(clazz);
        } catch (Exception e) {
            Log.w(TAG, e);
        }
        if (TextUtils.isEmpty(strRet)) {
            final int pid = android.os.Process.myPid();
            android.app.ActivityManager activityManager = (android.app.ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            final List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager
                    .getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo appProcess : runningAppProcesses) {
                if (appProcess.pid == pid) {
                    strRet = appProcess.processName;
                    break;
                }
            }
        }
        return strRet;
    }
}