本文介绍了查找广播接收器在运行Android活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从其他什么活动正在运行我的广播接收器中找到。这是code,我从一个活动用来寻找其他正在运行的activites但是当我尝试在我的广播接收机使用这个code我得到以下行错误:

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

显示在eclipse ACTIVITY_SERVICE错误不能被解析为一个变量

 软件包管理系统PM = this.getPackageManager();

这说明在Eclipse中错误的方法getPackageManager()是未定义的类型ScreenReceiver(我的广播接收器)

下面是完整的code:

 公共无效getRunning(){
        ActivityManager AM =(ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
        列表L = am.getRunningAppProcesses();
        迭代器I = l.iterator();
        软件包管理系统下午= this.getPackageManager();
        而(i.hasNext()){
          ActivityManager.RunningAppProcessInfo信息=(ActivityManager.RunningAppProcessInfo)(i.next());
          尝试{
            CharSequence的C = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));
            Log.w(LABEL,c.toString());
            runningApplications.add(c.toString());
          }赶上(例外五){
            //名称未发现异常
          }
        }    }


解决方案

由于广播接收器不从上下文血统,则不能使用这个,因为你可以在活动做。您应该使用传递到您的onReceive()方法上下文的实例。

 公共无效getRunning(上下文的背景下){
        ActivityManager AM =(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        列表L = am.getRunningAppProcesses();
        迭代器I = l.iterator();
        软件包管理系统下午= context.getPackageManager();
        而(i.hasNext()){
          ActivityManager.RunningAppProcessInfo信息=(ActivityManager.RunningAppProcessInfo)(i.next());
          尝试{
            CharSequence的C = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));
            Log.w(LABEL,c.toString());
            runningApplications.add(c.toString());
          }赶上(例外五){
            //名称未发现异常
          }
        }    }

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

 PackageManager pm = this.getPackageManager();

and this shows the error in eclipse The method getPackageManager() is undefined for the type ScreenReceiver (my broadcast receiver)

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. You should use the instance of Context that is passed to your onReceive() method.

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

    }

这篇关于查找广播接收器在运行Android活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 08:24