Wednesday 26 December 2012

Ok, lets put some code now ..

Start with the main UI activity, which will just show a toggle button. On clicking this button, the state of responsiveness of the app changes. We'll implement onClickListener on this button through the xml file.

The corresponding code in main.xml for the button should look like

<ToggleButton 
        android:id="@+id/appOnOff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="buttonStateChanged"/>

The android:id attribute specifies an unique id for this button, from which it is referenced in the java source files.

The android:onClick="buttonStateChanged" attribute handles the click events on the ToggleButton. A function declared as public with its return type as void and name buttonStateChanged will be called whenever the click event happens. So we store the state of the responsiveness of the app in a boolean variable in this function. 

The corresponding code in MainActivity.java should look like 


public void buttonStateChanged(View v) {
    ToggleButton appOnOff = (ToggleButton)findViewById(R.id.appOnOff);
    if(appOnOff.isChecked()){
    state = true;
    Toast.makeText(this, "Automatic response on", Toast.LENGTH_SHORT).show();
    }
    else{
    state = false;
    Toast.makeText(this, "Automatic response off", Toast.LENGTH_SHORT).show();
    }
}


The variable appOnOff carries the ToggleButton object which was declared in the layout file main.xml through its referenced id. Based on the state of button (isChecked returning true or false) the boolean variable state is initialized. This value will be used by the broadcast receiver to decide whether to take action or not.

Now lets add the Broadcast Receiver for the sms broadcasts. To receive a sms we have to add this permission in our androidManifest.xml file

<uses-permission android:name="android.permission.RECEIVE_SMS"/>


Wednesday 21 December 2011

App1, SMS Responder

The application SMS Responder works like a simple automated responder. Whenever you have enabled the Responder, for any incoming sms, a response will be automatically generated.

Goals :

1. The application should be able to send different texts to different senders which should be stored in some database.
2. The application should not occupy processing time/memory when not running.


These seem to be the most obvious goals of such application, however we start from a small activity and see as the growth follows.


So the primary target is to specify a UserInrterface which will allow a user to disable or enable the application, i.e.. when disabled no responses should be generated.


It seems like the ToggleButton is the perfect thing for such type of application. So the main UI activity will consist of a ToggleButton at the very least. We can customize it as and when required, lets just begin with a toggle button.

The second target is to somehow get informed whenever a SMS has arrived. There must be a broadcast intent for the purpose. Such a broadcast can be filtered by a BroadcastReceiver component.
I think we'd have to implement it. Also on second thought , it occurs that the application should consist of a service which always runs in the background and performs some action (respond to the SMS) when a SMS is received.

So we'll create broadcast receiver class (by inheriting the native BroadcastReceiver class) and set the appropriate filters for it. Again the question comes , how do we control the broadcast receiver from that toggle button. The filters for the broadcast reside in the manifest file, which as far as i knew, can't be changed at runtime. How then should I perform this task. The search for a solution led to many paths, some of which are :
a) You could use dynamic registration for registering the filters for broadcast receiver
b) You could use packagemanager class and its functions to enable or disable particular components of the application.
c) You could set a flag somewhere in the database and let the broadreceiver listen to every broadcast, only the action should be taken when that flag is set.

 Neither of these options seemed wrong, but the solution was very simple and can be done without any of the above methods. But first of all we should discuss all the above options, after all this is the way to learn new things.
The Dynamic registration couldn't have worked because it is enabled only when the activity in which the function is called is running, i.e., your user interface must be displayed at all the times. This is undesirable and occupies a lot of memory and processing.
The packagemanager class is a good option but i still haven't known all its functionality, so I'll better put that to the TODO list.
The flag in a database can also be set, but it'll require lookups in the database every time a SMS is received, which takes processing, also I am not yet very comfortable with handling databases.

So I tried another solution. I came to know that the state of the toggle button can be fetched by the broadcast receiver component. And voila, all my problems are solved. I'll just use a static boolean variable in the activity with toggle button, which will keep track of the state of the button. Whenever I receive a broadcast for a SMS, I simply check this variable, which is much less time consuming than lookup in database and is extremely simple.
I just create a simple broadcast receiver with filters for receiving SMS, and perform the required action (Responding to that SMS in this case)  based on the value of that boolean variable.
It does the trick.
So in this way this simple automatic action can be taken whenever a SMS is received. The remaining targets are :

1. Storing custom messages in database.
2. Fetching the senders no and responding with a SMS.

These will be dealt in forthcoming days.