Android 获得当前进程PackageName

如果上下文中没有Context对象,则用以下方法:

import android.os.Process;

Process.myTid()  ----------get current thread id

Process.myPid()  ----------get current Process id

ServiceManager.getService(Context.DISPLAY_SERVICE);


如果有Context,则可以用以下方法:

ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
             int processid = 0;
       for(int i = 0; i < pids.size(); i++)
       {
           ActivityManager.RunningAppProcessInfo info = pids.get(i);
           if(info.processName.equalsIgnoreCase("here your package name")){
              processid = info.pid;
           } 
       }

你可能感兴趣的:(Android)