Monitor Currently Running Application

一个人想着一个人 提交于 2019-12-02 17:56:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!