Hello I am trying to track a certain app after it has installed. I want to have a information of apps whenever they start.
It's just like AppLock
app in which they know when I open an app on which I activated a lock then it show their activity
to enter password.
To achieve this
I've made a service and put a while loop infinite in a gap of 1 seconds to keep track of running application on top but it really drain my battery in a few hours. AppLock
did this in a nice way that's why it doesn't drain battery fast.
I know that my current logic that is really bad:
while(true) {
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
Log.d("Executed app", "Application executed : " +recentTasks.get(0).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(0).id+"");
Thread.sleep(1000);
}
Question
How this should be done in a efficient way as like AppLock
app ?