2012-07-17 63 views
0

我想從我的廣播接收器中查找當前正在運行的其他活動。這是我從活動中使用它來查找其他運行activites但是當我嘗試在我的廣播接收器使用此代碼我得到以下行錯誤代碼:在Android中查找廣播接收器的運行活動

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

顯示在eclipse ACTIVITY_SERVICE誤差不能解析爲一個可變

PackageManager pm = this.getPackageManager(); 

,這表明在蝕該錯誤的方法getPackageManager()是未定義的類型ScreenReceiver(我的廣播接收機)

這裏是全碼:

public void getRunning(){ 
     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 { 
      CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA)); 
      Log.w("LABEL", c.toString()); 
      runningApplications.add(c.toString()); 
      }catch(Exception e) { 
      //Name Not Found Exception 
      } 
     } 

    } 

回答

4

由於廣播接收器沒有從上下文血統,你不能使用這個,你可以在活動做。您應該使用傳遞給onReceive()方法的Context實例。

public void getRunning(Context context){ 
     ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); 
     List l = am.getRunningAppProcesses(); 
     Iterator i = l.iterator(); 
     PackageManager pm = context.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()); 
      runningApplications.add(c.toString()); 
      }catch(Exception e) { 
      //Name Not Found Exception 
      } 
     } 

    } 
+0

要更加清楚這個問題:'this'指您正在操作的類實例。在這種情況下,是「BroadcastReceiver」的後代。所以'這個'與預期完全不同。 – yoshi 2012-07-17 20:00:35

+0

runningApplications.add(c.toString()); <---什麼類型的runningApplications? – bofredo 2013-08-29 13:38:06

1

嘗試爲:

public class ScreenReceiver extends BroadcastReceiver { 
private Context ctext; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     ctext=context; 
     //OR you can also pass context as param to getRunning() 
     //your code here.... 
    } 
public void getRunning(){ 
     ActivityManager am = (ActivityManager)ctext.getSystemService(Context.ACTIVITY_SERVICE); 
     List l = am.getRunningAppProcesses(); 
     Iterator i = l.iterator(); 
     PackageManager pm = ctext.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()); 
      runningApplications.add(c.toString()); 
      }catch(Exception e) { 
      //Name Not Found Exception 
      } 
     } 

    } 

} 
+0

謝謝你,解決的問題之一,但我仍然得到就行'ActivityManager AM =(ActivityManager)ctext.getSystemService(ACTIVITY_SERVICE)錯誤;'明確的話'ACTIVITY_SERVICE'我得到'ACTIVITY_SERVICE不能被解析爲變量# – Peter 2012-07-17 19:56:06

+2

使用Context.ACTIVITY_SERVICE – uaraven 2012-07-17 19:57:27

+0

@Peter:使用'Context.ACTIVITY_SERVICE'而不是''ACTIVITY_SERVICE' – 2012-07-17 19:59:43