Android Service + BroadcastReceiver, PhoneStateListener
I'm in the process of developing a phone applica开发者_开发技巧tion with a very simple GUI which initiates a service to run in the background.
This service will be responsible for recording data on a local database. This data will reportedly be anything with regards to user actions the Android API allows me to get.
This vary from, when a user accepts/reject call, by who, whether the phone was on silent, the location of the user and things like that.
another thing could be the time when the user put their phone on charge, switched the screen off, accessed an application, listens to music, is using the camera etc.
I'm not interested in the data they're using but rather than the actions they are doing.
I have found a couple of relevant classes one of being BroadcastReceiver and the other PhoneStateListener but I haven't be able to use them within my service.
I have a class (RecordEvents) which extends BraodcastReceiver and implements the onReceive method by checking the different actions (intent.getAction()) that come in. If it's one of those I'm interested I log it using Log.i().
Now within my service (BackgroundService class), which I can confirm it runs successfully, I instantiate RecordEvents and within the onCreate method I call registerReceiver(recordEvents, filter) recordEvents is an instance of RecordEvents and filter is an IntentFilter with all the intent actions I'm interested in.
Now If I perform any of the actions I'm interested in on the emulator/phone, no print message is shown on the log cat.
Am I doing something completely wrong?
Is BroadcastReceiver the right class to be looking into for recording user actions in a database in the background?
I'm also using another class (PhoneEvents) which extends PhoneStateListener overriding onCellLocationChanged, onDataActivity, onCallStateChanged and this class is instantiated and is used by TelephonyManager.listen(phoneEvents, PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_ACTIVITY | PhoneStateListener.LISTEN_CALL_STATE);
like that.
Still I get no logs when I install the app on a device.
I'd be grateful for your answers.
Thanks, Andreas
I'm not sure, but why do you use dynamic registering instead of static one? Just register the receiver within your manifest and bind to the service in onReceive()
. May be it will solve your problem.
精彩评论