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.
- Is that assumption correct?
- 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)