Sunday, October 11, 2015

simple BroadcastReceiver for Android missed calls app + tips for remote/wireless debuging on the phone

i wanted to play a bit with Android programming (again), so first i decided to try the latest AndroidStudio. I was originally Android-ing on Eclipse and gave only a quick test to the then-new AndroidStudio (neg, mostly because of Gradle probs, i think).
Now I see much has changed, AndroidStudio is very stable and doesn't get in the way - at all - even helping quite a bit.
The best improvement in the Android development environment, for me - and i don't know if it is nothing new - is the ability to run/test and debug (!) remotely - in my case wirelessly - on an actual physical device.

so no slooow Android emulator, no genymotion (which is actually good) - just write your code, immediately deploy/run/test/debug on a real device - so good!
basically what you have to do is plug the phone via usb and type
./adb tcpip 5555
then ask nicely adb to connect to your device (you must know the IP by which the device is connected to your same network, for example 222.778.1.21, but more likely something like 192.168.0.301):
./adb connect 222.778.1.21:5555
then unplug the phone, check if adb sees it via:
./adb devices
and off you go - start run/debug in AndroidStudio and select your phone/tablet/watch/car whatever from the list
this article helped a lot to clear this out - www.jessechen.net/blog/debugging-your-android-app-wirelessly-on-an-android-smartphone/

note that i don't think you actually need the apps that run your phone mentioned in this article, since it seems they only serve to show the IP of your device, which can be retrieved in other ways
 

coding the app itself was easy, except for a bug i made in the end, which caused some &^#$%&$^%$:
i open a cursor into the handy CallLog.Calls, but immediately did moveToNext(), which was jumping to the wrong record....blast.

i didn't bother for UI for the app, since everything is done behind the scenes with BroadcastReceiver and AlarmManager.
Programming was fun, there is so much info on the net that for simple things everyone should be able to write himself nowadays, programming is really quickly turning into a household skill so to say :) oh, what the future will bring with the internet of things!


just to get a glimpse of the code and approach:

we set a BroadcastReceiver for android.intent.action.PHONE_STATE and when notification arrives, we plug a listener into the call state and in its onCallStateChanged try to determine if the call was answered or not.
Then we set an alarm with AlarmManager and then we define a new receiver for the alarm event.
Now this is where we turn "professional" since the user might have reviewed the missed calls before the alarm rang, so we have to check this first, before ringing any bells for missed calls that the user is already aware of.

How? By opening the log and checking the status of missed calls - there is a flag "NEW" which is 1, if the user had not acted on the system missed calls notification, so we check if any of the call records in the log have this set, if yes - we play our own notification and set a new one, which will follow the same path of logic.
There is also another interesting flag/field there: IS_READ, but it seems it is set to 1 only when user acted on the call specifically, like calling back, from what i could gather. In any case flag new proved to be the reliable one.

i actually pushed the new product to github at https://github.com/esdee-git/Micalls, and might even try to push it to Google Play store, just to get a feel of the experience

No comments :