Category Archives: Android

Android – SMSReader

Trying to improve my Android understanding, and reading 01Informatique paper about TextToSpeech Android API, I wrote a little SMSReader application. This simple application allow you to start/stop a service that will read every received SMS. It can be usefull for example when you are driving !

It allows me to be introduced to Android android.app.Service and android.app.BroadcastReceiver components. It also give me a chance to try the application state save/recover concept that is hidden behind the android.app.Activity component.

Global Architecture is simple. It is composed of three components :

  • SMSReaderSpeakerService that is responsible for performing the SMS speak
  • SMSReaderActivity that is responsible for starting the user service by activating the SMSReaderSpeakerService
  • SMSReaderReceiver that is responsible for receiving SMS_Received broadcast event and pass it to the SMSReaderSpeakerService.

SMSReaderSpeakerService inherits from android.app.Service component. This kind of components are long-life component that can be started and perform background operations. My first version of the SMSReader tried to perform the speaking operation on the BroadcastReceiver (SMSReaderReceiver) component. But executing time-consuming operation like TextToSpeech can not be done on such component. Their life have to be short !

So here is the SMSReaderSpeakerService. It can handle three actions declared in the AndroidManifest.xml file :

<service android:name=”.SMSReaderSpeakerService”>
<intent-filter>
<action android:name=”com.mrlonee.SMS_SPEAK” />
<action android:name=”com.mrlonee.SMS_SPEAKER_ACTIVE” />
<action android:name=”com.mrlonee.SMS_SPEAK_CONFIGURATION” />
</intent-filter>
</service>

It means that sending one of these message to this service will do something ! SMS_SPEAK is to perform context messages Speak. SMS_SPEAKER_ACTIVE is to activate/desactivate the service for user (through the SMSReaderActivity component). SMS_SPEAK_CONNFIGURATION can be used to configure the TextToSpeech language that will be used.

SMSReaderActivity is the simple one toggle button HMI. It only own a toggle button that send and SMS_SPEAK_ACTIVE Itent to the SMSReaderSpeakerService Service, adding a parameter value that is a boolean indicating if the service is started or not. Here is the main code :

<em>if(toggleButton.isChecked()){
Intent speakerIntent = new Intent("com.mrlonee.SMS_SPEAKER_ACTIVE");
speakerIntent.putExtra("ACTIVE", true);
this.getApplicationContext().startService(speakerIntent);
}
else{
Intent speakerIntent = new Intent("com.mrlonee.SMS_SPEAKER_ACTIVE");
speakerIntent.putExtra("ACTIVE", false);
this.getApplicationContext().startService(speakerIntent);
}</em>

I tried to perform activity state save/restore mechanism, because an activity can be killed when your operating system needs some resources. It was a pain in the ass to manage to do so. I thought it would be simple, as the activity component life cycle seemed to be clear for me, but I discovered that it was difficult to manage all cases (creation, restore, close, background) and transitions. Finally the code that worked for me in most case was the onSaveInstanceState()/onRestoreInstanceState() duet. But I know it does not cover all the cases.

SMSReaderReceiver is a Broadcast receiver I simply declared in the AndroidManifest.xml file as a android.provider.Telephony.SMS_RECEIVED action consumer using the following receiver section :

<receiver android:name=”.SMSReaderReceiver”>
<intent-filter>
<action android:name=”android.provider.Telephony.SMS_RECEIVED” />
</intent-filter>
</receiver>

Its only work is to decode the incming SMS_RECEIVED parameters to pass it to the SMSReaderSpeakerService in an Intent with messages in parameters like this :

@Override
public void onReceive(final Context context, final Intent intent){

List<String> messages = new ArrayList<String>();
Bundle bundle = intent.getExtras();
if (bundle != null){
//—retrieve the SMS message received—
Object[] pdus = (Object[]) bundle.get(“pdus”);
for (int i=0; i<pdus.length; i++){
StringBuilder str = new StringBuilder();
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])pdus[i]);
if(smsMessage.isEmail()){
str.append(“Mail de “);
}
else{
str.append(“SMS de “);
}

str.append(smsMessage.getOriginatingAddress()).append(“. “).append(smsMessage.getMessageBody().toString());

messages.add(str.toString());
}

Intent speakerIntent = new Intent(“com.mrlonee.SMS_SPEAK”);
speakerIntent.putExtra(“SMS”, messages.toArray(new String[messages.size()]));
context.startService(speakerIntent);
}
}

To finish I would say that the components responsibilities are easy to identify in the way android propose to structure your application. Activity for HMI, Service for Background performed operations and BroadcastReceiver for system and others applications event receiving. Intent can be used to communicate through components. What despite me is the fact that the component communication is done using a key/value interface object. You can not be sure when you write your application that the components interface are the one you expect. You will discover this at runtime ! I would prefer having something like injection between components that allow you to perform compiled interface call instead of using such key/value system. It seems to me that this way of communication is safer.

Anyway, you can find the whole Eclipse project in a zip file here ! You will need Android Eclipse plugin to look at the SMSSpeaker project. The .apk file is also available here.

This program could be improved by replacing the SMS originator number by its name in the phone contacts list if it exists ! Help yourself, and send me your improvement !

LoNee

Android – First Impressions

My first impression about Android development is good. First installing the environment to develop in eclipse is quit easy and well documented. I was coding and debugging my first application in 20 mn !

I also appreciate the way they tried to help development by offering pre-defined component type for separated concern. Activities, Services, Broadcast Receiver, Content provider. I will focus on Activity for now, because it the only one I’ve really used.

Activity definesĀ  a visual component to display a frame and offers you simple way to manage your application life cycle. In my opinion this point is very important, because code to manage application life cycle is in most case very boring, but also very important for your application behaviour. Managing your application life cycle is done by overriding some methods. Here is a link that explain you when theses methods are called in the Activity component life cycle (see Activity Lifecycle).

For the graphic side, defining your UI content using XML description file is not my cup of tea. I appreciate code completion and human readable code. XML file is such a mess to read and maintain.

Root graphic class View is for me problematic. First they defined Layout as particular view. I do not really get why melting these two aspects of UI building under one same root object. Layout views only manage child view layering ? So why not separating concerns ?

Then I don’t really get why Context object are always needed when creating any View objet. When I read the summary of the class I see :

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

Why does every view needs such object ? Does a simple graphic view element will access application-specific resources or perform up-calls for application-level operation ? when I look at the MVC pattern, Views are some kind of passive element that are feeded and listened by the controller !

Talking about listener, I wonder if views are writtent to manage multi-listener. Looking at the setOnClickListener() method on view make me wondering. What does it mean, you will have to manage every view on click event in the same listener that will have to dispatch the event to every really interested object ?

Well, I will continue using Android and try to develop some simple application. It will allow me to go further in discovering this new development environment. I started to develop a Guitare Tuner. For now I only begin and have nothing to propose for download, but I will give you some code soon !

LoNee

Android – Here I Am

I plan to give a try to android application development. Many reason to that choice :

  • I thing the business model can be interesting for independent developers
  • I like the idea of little programs that can ease every day’s life.
  • I like the idea of having my own creation with me every time everywhere
  • I want to become rich
  • my wife owns a new Samsung Galaxy S phone with android 2.2 OS :)

Coming soon my first program that should be a guitar tuner ! I will also expose my opinion on android development environment and API.

LoNee