问题
I am encountering a problem that i can't not solve for the moment.
The purpose of the code is to monitor which applications are running at current moment.
I used the following code and logged the resulting package name, it worked.
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
Log.i("TTWYMonitor", packageName);
But I use that code in a BroadcastReceiver
, nothing happened.
In manifest
,I declared an intent receiver android:name=".MonitorApplication.
What should I do, then?
Please give any suggestion.
Yahel : Thanks and sorry for my informal question.
回答1:
replace the "Activity" in getSystemService's parameters with "Context":
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
I tested it and works fine for me!
来源:https://stackoverflow.com/questions/6420767/monitor-currently-running-application