Advertisment

Android Tutorial: Is it Night or Day, Dark or Light Over There?

author-image
PANKAJ
New Update

Note: If you need the .apk file of the source code below, then place your request at Ask PCQ and we'll mail it across to you.

Advertisment

I will discuss developing one such application for an Android phone. This application is an extension of the safety application I introduced in the December 2013 issue of PCQuest with the title "Android Programming: Auto-Check the Location of Your Friends and Family!", accessible at http://tinyurl.com/lunf59d.

The Requirement

We will develop an application that will run on your mobile and respond to queries on how much light is there around you. Because the person sending the query will use SMS, he need not have this particular application installed, for that matter, the sender need not even have an Android phone. He needs to only have a handset that can send and receive SMS. When he sends a message with the message text #HowMuchLight#, this application will intercept the message and use the light sensor to find out the amount of light and reply back.

I will extend the location check application I already discussed to provide this additional functionality. One reason is I want to reuse the code that I have already developed to receive and check SMS for specific text and respond back with information. The other reason is logically the two functionality belong together in the area of safety -someone worried about you gets valuable information by knowing your location and the amount of light around you.

The Solution

First check that you have a light sensor available on your handset. You could check your handset's documentation, or ask your favorite search engine if your model supports a light sensor. If you follow through this article and search Android documentation, you should also be able to programmatically check if your handset has a light sensor.

Using Windows Explorer, copy the project folder WhereAreYou and name the copy SafetyMessages. Import the newly created project into your Eclipse workspace. Open the SMSReceiver class. Define a constant for the message text.

public final String HOWMUCHLIGHT = "#HowMuchLight#";

Inside the for loop in onReceive(), create an if condition stub if the new message is received, parallel to the if condition for the location message.

if (strMessage.equals(HOWMUCHLIGHT)) {

}

We would need a service that would run to start the listener for the sensor. Create a new class LightService that extends the Service class. Add the onCreate() function to the service class.

public void onCreate() {

super.onCreate();

}

Also add the onStartCommand() to the service. We will make the service sticky so that the listener will continue to run.

public int onStartCommand(Intent intent, int flags, int startId) {

super.onStartCommand(intent, flags, startId);

return START_STICKY;

}

In the receiver class, create a method that processes the messages and starts the relevant service.

private void processMessage(Context context, Class < ?> cls, String strFrom) {

Intent itnStart = new Intent(context, cls);

itnStart.putExtra(PHONENUMBER, strFrom);

context.startService(itnStart);

}

Modify the two if conditions so that they call the method we just created.

if (strMessage.equals(WHEREAREYOU)) {

processMessage(context, LocationService.class, strFrom);

}

if (strMessage.equals(HOWMUCHLIGHT)) {

processMessage(context, LightService.class, strFrom);

}

Add the service LightService to the manifest. Run the application to check you are getting proper response to the where are you message.

Add a variable to store the number of the sender in the service class.

private String strTo;

Add code to onStartCommand() of the service to extract the phone number.

Bundle bunSMS = intent.getExtras();

strTo = bunSMS.getString(SMSReceiver.PHONENUMBER);

Add code to onStartCommand() to send a dummy SMS. Before we code the sensor, let us at least check if the SMS infrastructure is operating well.

SmsManager smsLight = SmsManager.getDefault();

smsLight.sendTextMessage(strTo, null, "Work in progress", null, null);

stopSelf();

Run the application and check you are getting expected responses to both where are you and how much light messages. You should get the right location in response to where are you and should get the message "Work in progress" in response to how much light message.

Create a variable in the service class to hold the

sensor manager.

private SensorManager smrLight;

Create a variable to store the light and create the sensor listener.

private float fltLight = Float.NaN;

private final SensorEventListener selLight = new SensorEventListener() {

public void onAccuracyChanged(Sensor sensor, int accuracy) { }

public void onSensorChanged(SensorEvent event) {

fltLight = event.values<0>;

}

};

Register the listener in onStartCommand and move the code to send SMS to its separate method.

public int onStartCommand(Intent intent, int flags, int startId) {

super.onStartCommand(intent, flags, startId);

Bundle bunSMS = intent.getExtras();

strTo = bunSMS.getString(SMSReceiver.PHONENUMBER);

smrLight = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

Sensor snrLight = smrLight.getDefaultSensor(Sensor.TYPE_LIGHT);

if (snrLight != null) smrLight.registerListener(selLight, snrLight, SensorManager.SENSOR_DELAY_NORMAL);

return START_STICKY;

}

public void sendMessage() {

SmsManager smsLight = SmsManager.getDefault();

smsLight.sendTextMessage(strTo, null, "Light:" + fltLight, null, null);

smrLight.unregisterListener(selLight);

stopSelf();

}

Call the method in the listener.

private final SensorEventListener selLight = new SensorEventListener() {

public void onAccuracyChanged(Sensor sensor, int accuracy) { }

public void onSensorChanged(SensorEvent event) {

fltLight = event.values<0>;

sendMessage();

}

};

Run the application and check for both where are you and how much light messages.

You can change the reply message to something friendlier if you like.

smsLight.sendTextMessage(strTo, null, "The light around me is:" + fltLight + "lux, or, lumen per square metre", null, null);

Run the application and check it is responding properly to both the messages.

We are done with the application now. You may want to add more security features like respond to the query only if the sender is from a pre-approved list of numbers. You can even consider adding more features like querying for the atmospheric pressure and temperature etc., depending on what sensors are available on your handset.

More Android Programming Tutorials:

Transferring Call Logs to Calendars in Android

Create a Sleep Time App to Auto-Send Notifications to Callers

Auto-Check the Location of Your Friends and Family!

Creating an Android Service to Copy Call Log to a Calendar

Advertisment