Tuesday, April 17, 2012

Intercept SMS with Broadcast Receiver

Hi developers,

in Android Broadcast Receivers are used to listen for broadcast Intents. A Broadcast Receiver needs to be registered, either in code or within the application manifest. (in this case is registered in the manifest)
When registering a Broadcast Receiver you must use an Intent Filter to specify which Intents it is listening for.

We use BroadcastReceiver to intercept SMS.
See the code below

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSBroadcastReceiver extends BroadcastReceiver{
 public void onReceive(Context context, Intent intent) {
  Bundle bundle = intent.getExtras();
  SmsMessage[] msgs = null;
  StringBuilder str = new StringBuilder();
  if (bundle != null) {
   // --- retrieve the received SMS here ---
   Object[] pdus = (Object[]) bundle.get("pdus");
   msgs = new SmsMessage[pdus.length];
   for (int i = 0; i < msgs.length; i++) {
    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
    str.append("SMS from " + msgs[i].getOriginatingAddress());
    str.append(" :");
   // ...we show sms here...
   Toast.makeText(context, str.toString(), Toast.LENGTH_SHORT).show();

As you can see, we extends from BroadcastReceiver and override the method onReceive where we retrieve information about the received SMS.

In the manifest you must define the RECEIVE_SMS uses-permission, register the receiver (our class SMSBroadcastListener) and his intent-filter (SMS_RECEIVED):




They will be started automatically when a matching Intent is broadcast, in practice when received an SMS you will see a Toast with SMS information.

Thursday, April 5, 2012

Tuesday, April 3, 2012

Where am I? Finding your location

One of the coolest thing that you can do with your Android device, is find your location!
In Android there are location-based services to get the current location of device. There are two ways to get the physical location:

  • Network Provider
  • GPS Provider
By LocationManager object we can retrieve latitude and longitude of last known location.
Now with these information and using Geocoder we get the addresses in the neighbours. Here's the code snippet:
// max of adresses near you
int max_result = 1;
// you can decide if use a network or gps provider
String provider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// get the last know location
Location location = locationManager.getLastKnownLocation(provider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
List<address> addresses = null;
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
     addresses = gc.getFromLocation(latitude, longitude, max_result);
} catch (IOException e) {}
// return the string information
Toast.makeText(this, addresses.get(0).toString(), Toast.LENGTH_LONG)
This example assumes:

  •  that you have enabled the NETWORK_PROVIDER
  •  so you can add in the manifest file the ACCESS_FINE_LOCATION uses-permission