Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.app.ActivityManager;

import android.content.Context;

import android.text.TextUtils;

import java.util.List;

public class Main {
    public static boolean isMainProcess(Context context) {
        // TODO if mainProcess is not packageName, you should modify here
        String mainProcess = context.getPackageName();
        return TextUtils.equals(getProcessName(context, android.os.Process.myPid()), mainProcess);
    }

    public static String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
            if (procInfo.pid == pid) {
                return procInfo.processName;
            }
        }
        return null;
    }
}