2

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 ?

N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • Umm. not a solution, but as a work around, you can try to use this code only when device screen is ON. with a broadcast receiver for `android.intent.action.SCREEN_ON`. And if [this](http://stackoverflow.com/a/7239840/1777090) makes sense, you can try – MysticMagicϡ Dec 19 '14 at 11:57

1 Answers1

0

There is an Action called android.intent.action.PACKAGE_RESTARTED.

So add an Broadcast reciver like

    <receiver android:name="your.package.PackageRestartedReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_RESTARTED"/>
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>

Add your onRecive function and you are good to go.

Eun
  • 4,146
  • 5
  • 30
  • 51
  • I added this in my application then I open the facebook then this broadcast receiver is not getting called. I removed this ` ` not sure what to write in package. – N Sharma Dec 15 '14 at 16:36
  • idk this answer is correct or not, but this answer means you should write `your.package.PackageRestartedReceiver extends BroadcastReceiver` class. and `` does not need to change. – ytRino Dec 17 '14 at 03:53
  • @ytRino I did but this never execute :( – N Sharma Dec 17 '14 at 05:18