0
String callerPackage = getAppNameByPID(getContext(), Binder.getCallingPid());
private String getAppNameByPID(Context context, int callingPid) {
//How ?
    ....

    }

私の質問:pidでアプリパッケージ名を取得する方法は?

4

2 に答える 2

2

各プロセスは複数のパッケージを保持できるため、次のようになります。

private String[] getPackageNames(int pid)
{
    ActivityManager activityManager = (ActivityManager)getContext().getSystemService(Activity.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
    for(RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) 
    {
        try 
        { 
            if(runningAppProcessInfo.pid == pid)
            {
                return runningAppProcessInfo.pkgList;
            }
        }
        catch(Exception e) 
        {
        }
    }
    return null;
}
于 2015-06-03T07:38:21.850 に答える
-1

こんにちは、このコードを試すことができます。私にとっては問題なく動作します。

private String getAppName(int pID)
{
    String processName = "";
    ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
    List l = am.getRunningAppProcesses();
    Iterator i = l.iterator();
    PackageManager pm = this.getPackageManager();
    while(i.hasNext()) 
    {
          ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
          try 
          { 
              if(info.pid == pID)
              {
                  CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
                  //Log.d("Process", "Id: "+ info.pid +" ProcessName: "+ info.processName +"  Label: "+c.toString());
                  //processName = c.toString();
                  processName = info.processName;
              }
          }
          catch(Exception e) 
          {
                //Log.d("Process", "Error>> :"+ e.toString());
          }
   }
    return processName;
}
于 2013-01-05T03:43:13.347 に答える