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;

public class Main {

    public static boolean isInMainProcess(Context pContext) {
        return pContext.getPackageName().equalsIgnoreCase(getCurrentProcessName(pContext));
    }

    public static String getCurrentProcessName(Context pContext) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) pContext.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return "";
    }
}