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
}
}
}
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
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.