2013-10-29 111 views
0

可能只有正在運行的應用程序的名稱而不是程序包名稱?我現在有這樣的代碼:正在運行的應用程序名稱而不是程序包名稱

ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE); 
     List<RunningAppProcessInfo> processes = am.getRunningAppProcesses(); 

     if (processes != null){ 
      for (int i=0; i<processes.size(); i++){ 
       RunningAppProcessInfo temp = processes.get(i); 
       String pName = temp.processName; 
       Log.d("Running process: ", pName); 
      } 
     } 

但它返回的包名稱如:com.android.camera。我只需要camera或例如Nova launcher

+1

檢查此[SO](http://stackoverflow.com/questions/3861073/get-active-application-name-in-android) –

+0

解決謝謝你! –

回答

1

試試這個:

final PackageManager pm = getApplicationContext().getPackageManager(); 
ApplicationInfo ai; 
try { 
    ai = pm.getApplicationInfo(this.getPackageName(), 0); 
} 
    catch (final NameNotFoundException e) { 
    ai = null; 
} 
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)"); 

在其清單的標籤來定義這將返回應用程序的名稱。

相關問題