0

My app has a background Service. In the Service I need to detect and log all app launches. For example: whether the user opens Facebook or Google+ or Twitter (any app) - I want a receiver in my Service to catch it for me to perform an action.

The only way I have been able to come up to do this - is to have a Timer running in the onCreate() function of my Service. My concern is that this 1 second timer may drain battery.

  1. Is that assumption correct?
  2. If yes, is there another way (some intent filter?) that I can register with my Broadcast Receiver to catch any App Launch?

Things I have tried:

(1) My Service code with the TimerTask. My trigger action code will be placed inside the "run()" function of the TimerTask.

public class KillService extends Service {

ActivityManager mActivityManager;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}



@Override
public void onDestroy() {
    unregisterReceiver(receiver);
    super.onDestroy();
}




@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();
    Timer timer = new Timer();    
            TimerTask refresher = new TimerTask() {
            public void run() {
                   //PLACING MY TRIGGER ACTION HERE
        };
    };
    //TIMER RUNS EVERY 1 SECOND
    timer.scheduleAtFixedRate(refresher, 1000,1000);    

}   
}

(2) Reviewed Available Broadcast Actions I reviewed the broadcast_actions.txt that comes in the SDK Folder (sdk\platforms\android-19\data), but I did not find any Intent that will be appropriate for this use case. Link to file

All I want is to know when any App Activity is started (i.e. in the Foreground) so that I dont have to continually check with Timer (afraid that it may drain battery)

halfer
  • 19,824
  • 17
  • 99
  • 186
user1406716
  • 9,565
  • 22
  • 96
  • 151

1 Answers1

0

Perhaps my answer in this question might help you. Android how to know an app has been started and range apps priority according the starting times

Place that code inside your background service.

Based on the package name of the activity in the foreground, you can detect that app name by checking all the apps on the phone and matching it with the app that has the same package name.

Edit

Since you want a continuous check, you could use an AsyncTask(), and in the doInBackground() function, just keep getting the list of running activities in each iteration, like I have suggested in that link, and the first item in the taskInfo list will have the activity/app in the foreground. You can also provide another button in your app which on click you stop this , by keeping a bool variable in shared preferences (which you poll for in the while condition in doInBackGround() function of the AsyncTask()).

I have done something similar in the past, and it worked for me.

halfer
  • 19,824
  • 17
  • 99
  • 186
SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • I know how to get the Foreground App package name, trouble is where in the Service do I put it? If I put it in the onCreate(), the foreground activity is only checked once. I want it to be checked every time an app/activity is launched. Am I missing something here? – user1406716 Feb 05 '14 at 10:31
  • So basically you want to have a track of all the apps that are activated since your app launches right? – SoulRayder Feb 05 '14 at 10:32
  • Yes. Did I interpret your answer correctly that - if i place your code in the onCreate() of my Service, then it will run only once... but i need to keep logging any time any app is launched by the user. – user1406716 Feb 05 '14 at 10:36
  • Try with Asynctask like I have suggested. – SoulRayder Feb 05 '14 at 10:41
  • I don't know if this is the best way. But it certainly works, and if you want to continuosly monitor something, something like this or what you have tried beforehand *has* to be adopted. – SoulRayder Feb 05 '14 at 10:44
  • Your method works, but i will go with the Timer as that may use less battery. I have marked your answers as the answer though since it works. I am still testing. I also added in the logic to run the timer only when a SCREEN_ON intent action is fired and to stop service when SCREEN_OFF happens. – user1406716 Feb 06 '14 at 01:20
  • Yes certainly, the method that you just suggested will work even better I guess :) – SoulRayder Feb 06 '14 at 03:34
  • Does `doInBackGround` need fixing to `doInBackground`? – halfer Aug 10 '22 at 22:49