2012-06-20 76 views

回答

10

在這裏你去

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); 
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED); 
Iterator i = l.iterator(); 
PackageManager pm = this.getPackageManager(); 
while (i.hasNext()) { 
    ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next()); 
    try { 
     CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
     info.processName, PackageManager.GET_META_DATA)); 
     Log.w("LABEL", c.toString()); 
    } catch (Exception e) { 
     // Name Not FOund Exception 
    } 
} 
+1

嘿謝謝你..你可以建議,哪些android用戶權限是需要這行 - info.processName,PackageManager.GET_META_DATA)); –

+0

嘿,我做了..謝謝你Vipul Shah。 –

+0

你能編輯這個答案並在這裏發表嗎? –

-1

有了這個,你可以得到當前的應用程序名稱

Resources appR = getApplicationContext().getResources(); 
CharSequence txt = appR.getText(appR.getIdentifier("app_name","string", getApplicationContext().getPackageName())); 
System.out.println(txt+" APp Name"); 
+0

獲得活動嘿嘿感謝您的回覆,但讓我知道,如果我在後臺運行這個,那麼它給出了當前在屏幕上顯示的其他打開的應用程序名稱? –

1

你也可以列出正在運行的任務用下面的代碼:

ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE); 
List li = am.getRunningTasks(100); 
Iterator i = li.iterator(); 
PackageManager pm = getApplicationContext().getPackageManager(); 
while (i.hasNext()) { 
    try { 
     ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo)(i.next()); 
     String ac = info.baseActivity.getPackageName(); 
     CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
     ac, PackageManager.GET_META_DATA)); 
     Log.v("asd", c.toString()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
0

對我來說上面的例子不起作用。所以,我結束了使用此:

ActivityManager am = (ActivityManager) this 
      .getSystemService(ACTIVITY_SERVICE); 

    List<ActivityManager.RecentTaskInfo> l = am.getRecentTasks(1, 
      ActivityManager.RECENT_WITH_EXCLUDED); 
    Iterator<ActivityManager.RecentTaskInfo> i = l.iterator(); 

    PackageManager pm = this.getPackageManager(); 

    while (i.hasNext()) { 
     try { 
      Intent intent = i.next().baseIntent; 
      List<ResolveInfo> list = pm.queryIntentActivities(intent, 
        PackageManager.MATCH_DEFAULT_ONLY); 

      CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
        list.get(0).activityInfo.packageName, 
        PackageManager.GET_META_DATA)); 

      Toast.makeText(this, "Application name: " + c.toString(), 
        Toast.LENGTH_LONG).show(); 

     } catch (Exception e) { 
      Toast.makeText(this, 
        "Application name not found: " + e.toString(), 
        Toast.LENGTH_LONG).show(); 
     } 
    } 
+2

從API Level 21開始,ActivityManager .getRececntTask已棄用! –

-1

只需使用此代碼

getActivity().getApplicationInfo().packageName 

它給你的應用程序信息,然後調用包的名稱。

0

使用的AccessibilityService

  • 您可以通過使用AccessibilityService檢測當前活動窗口。
  • 在onAccessibilityEvent回調中,檢查TYPE_WINDOW_STATE_CHANGED事件類型以確定當前窗口何時更改。
  • 通過調用PackageManager.getActivityInfo()來檢查窗口是否爲活動。

我測試並通過Android 7.1(API 25)在Android 2.2(API 8)中工作。

public class MyAccessibilityService extends AccessibilityService { 
    @Override 
    protected void onServiceConnected() { 
     super.onServiceConnected(); 

     //Configure these here for compatibility with API 13 and below. 
     AccessibilityServiceInfo config = new AccessibilityServiceInfo(); 
     config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED; 
     config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC; 

     if (Build.VERSION.SDK_INT >= 16) 
      //Just in case this helps 
      config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS; 

     setServiceInfo(config); 
    } 
    @Override 
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) { 
     Log.d("ABC-",accessibilityEvent.getPackageName()+" -- "+accessibilityEvent.getClassName()); 
     if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { 
      if (accessibilityEvent.getPackageName() != null && accessibilityEvent.getClassName() != null) { 
       ComponentName componentName = new ComponentName(
         accessibilityEvent.getPackageName().toString(), 
         accessibilityEvent.getClassName().toString() 
       ); 

       ActivityInfo activityInfo = tryGetActivity(componentName); 
       boolean isActivity = activityInfo != null; 
       if (isActivity) 
        Log.i("CurrentActivity", componentName.flattenToShortString()); 
      } 
     } 
    } 

    private ActivityInfo tryGetActivity(ComponentName componentName) { 
     try { 
      return getPackageManager().getActivityInfo(componentName, 0); 
     } catch (PackageManager.NameNotFoundException e) { 
      return null; 
     } 
    } 
    @Override 
    public void onInterrupt() { 

    } 
} 
相關問題