Sunday, January 25, 2015
Android beginner tutorial Part 65 Receiving system broadcasts
The Android system sends out broadcasts just like applications do. They are Intents that can be caught by BroadcastReceivers. Using a receiver, we can listen to actions such as phone calls, SMS text messages, battery status change, timezone change and so on.
In this tutorial were going to catch an SMS text message and display it using a Toast.
Were only going to use one Application, which will have the BroadcastReceiver.
Go to the AndroidManifest.xml file of that application and declare a new BroadcastReceiver. Set its filters action field to android.provider.Telephony.SMS_RECEIVED. The android.provider.Telephony class is used by the system and isnt a part of the Public API Android SDK, so we cant refer to it directly. But we can still simply set the value to a string, like shown below:
<receiver android:name="TestReceiver" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
After the application tags, add permissions for reading and receiving SMS:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
Now the TestReceiver will respond to new SMS.
Open TestReceiver.java class. Inside the onReceive() function well use the intent.getAction() value and compare it to "android.provider.Telephony.SMS_RECEIVED" to make sure that were responding to the correct Intent.
Extract the extras out of the Intent, create a new StringBuilder object, which will let us compose a message for the Toast out of multiple strings.
Check if the bundle isnt null. If it isnt, create a new array of objects out of "pdus" object from the extras. This is an array that contains data of the received message. The pdu itself isnt used though, first it is convered to an understandable format using createFromPdu() method of the SmsMessage class.
Use the getOriginatingAddress() and getMessageBody() methods of the SmsMessage object to get the sender and the text.
Display it all using a Toast.
package com.example.codeforfoodtest_two;
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 TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle extras = intent.getExtras();
StringBuilder sb = new StringBuilder();
if(extras!=null){
Object[] pdus = (Object[])extras.get("pdus");
for(int i=0; i<pdus.length; i++){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append("
From: " + smsMessage.getOriginatingAddress());
sb.append("
From: " + smsMessage.getMessageBody());
}
}
Toast.makeText(context, "New SMS message" + sb.toString(), Toast.LENGTH_LONG).show();
}
}
}
Thats all!
After installing the application on your device, whenever you receive SMS messages they will be displayed in a Toast.
If you test this on an emulator, you can use DDMS to send mock sms messages to your emulator. To do that, open th DDMS tab in Eclipse, select your emulator/device in the left side of the screen, then open the Emulator Control tab on the right, set the Incoming number to 5554 (or whatever your emulators port number is - it is displayed near the emulators name). Select SMS and write a message, then hit Send.
Thanks for reading!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.