监视当前运行的应用

2023-09-04 06:00:20 作者:你和氧气同起坐

我遇到了,我不能没有解决暂时的问题。 的code的目的是要监视的应用程序是在当前时刻运行。 我用下面的code和记录所产生的包名称,它的工作。

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);

但我用code在的BroadcastReceiver ,什么都没有发生。 在清单,我宣布意图接收器安卓。NAME =MonitorApplication 我该怎么办呢? 请给出任何建议。

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 :谢谢,对不起我的非正式的问题。

Yahel : Thanks and sorry for my informal question.

推荐答案

更​​换活动,在getSystemService的参数有背景:

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!

相关推荐
 
精彩推荐
图片推荐