简体   繁体   English

从Android的广播接收器中查找跑步活动

[英]Find Running activities from a Broadcast Receiver in Android

I want to find from within my broadcast receiver what other activities are currently running. 我想从广播接收器中查找当前正在运行的其他活动。 This is the code that i use from an activity to find the other running activites but when i try to use this code in my broadcast receiver i get errors on the following lines: 这是我从活动中使用的代码,用于查找其他正在运行的活动,但是当我尝试在广播接收器中使用此代码时,在以下几行中会出错:

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

shows the error in eclipse ACTIVITY_SERVICE cannot be resolved to a variable 显示日食中的错误ACTIVITY_SERVICE无法解析为变量

 PackageManager pm = this.getPackageManager();

and this shows the error in eclipse The method getPackageManager() is undefined for the type ScreenReceiver (my broadcast receiver) 这显示了eclipse中的错误。对于类型ScreenReceiver(我的广播接收器),未定义方法getPackageManager()

Here is the full code: 这是完整的代码:

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
          }
        }

    }

Since BroadcastReceiver does not descent from a Context, you cannot use this , as you can do in Activity. 由于BroadcastReceiver不会从Context下降,因此您无法像在Activity中那样使用this You should use the instance of Context that is passed to your onReceive() method. 您应该使用传递给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
          }
        }

    }

try as: 尝试为:

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
          }
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM