Reputation: 43
I'm trying to make an app which records usage of various apps on the device. e.g: 10 mins for Fifa, 23 mins for GTA and etc. right now i have service which runs constantly and i have a broadcast receiver which suppose to trigger when any application package changes( using intent.action.PACKAGE_CHANGED), but unfortunately code inside the broadcast never runs. i appreciate if any one could hint out the problem or tell me any other ways to do this. Thanks
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("BROADCAST", "Works fine" );
}
}
and this is the code inside the Service class:
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_CHANGED);
ScreenReceiver recevier = new ScreenReceiver();
registerReceiver(recevier, filter);runningApps();
return START_STICKY;
}
Upvotes: 2
Views: 1863
Reputation: 115942
PACKAGE_CHANGED intent will only be sent when the app iteself changes . it has nothing to do with app usage .
in fact , there is no official way to monitor apps usage . there are only similar solutions:
up to jelly bean , you could read the logs and find out when a process of an app has started and when it closed (or user moved to another app) .
you can poll every now and then about the recently launched apps or the currently running processes .
you can create an launcher app that will find out which apps are being launched within it.
of course , each of the methods i've written has its flaws and only give you a clue about the real usage , and of course since android supports running more than one app at the same time , i don't think any method would suffice.
Upvotes: 1