how do android monitor usage applications work

梦想的初衷 提交于 2019-12-01 01:03:38

The only way to monitor other apps is by monitoring foreground activity. This will drain your battery but there is no broadcast when other apps are launched and hence no other way to do it. You would like to start a timer task that checks the current foreground activity. I would highly recommend not to do it. To get current foreground activity use the following code:

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1); 
Log.d("topActivity", "CURRENT Activity ::"
            + taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();

You will need the following permission:

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