|
|
Broadcast Receiver
In this project we make an application which will be able to
broadcast a event say a message to a sender when it receive a particular type
of event say a particular type of message.
We extend this application to the application we made to
send a message.
1)create a new java
file named as ReceiveSms( in the package which we use in developing application
for sending a sms) and extends with BroadcastReceiver and match it as:
package com.sartaj.sms; //package name
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import android.os.Bundle;
import
android.telephony.SmsManager;
import
android.telephony.SmsMessage;
public class RecieveSms extends BroadcastReceiver {
public void onReceive(Context
arg0, Intent arg1) {
// TODO Auto-generated
method stub
// ---get the SMS
message passed in---
Bundle
bundle = arg1.getExtras();
SmsMessage[]
msgs = null;
String
str2 = "";
if (bundle != null) {
// ---retrieve the
SMS message received---
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]);
str2
= msgs[i].getOriginatingAddress();
}
SmsManager
sm = SmsManager.getDefault();
sm.sendTextMessage(str2,
null, "I m in
Metting", null, null);
}
}
}
2)Add the permission(android.permission.RECEIVE_SMS)in
AndroidManifest.xml also add the receiver attribute in application attribute of
AndriodManifest.xml:
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sartaj.sms"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9"
/>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".Sms"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<receiver android:name="RecieveSms">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
3)Now run the application on emulator
say 5554.Create another emulator say 5556 .When we send a message from 5556 to
5554 it will automatically send a message to the sender that is 5556.
0 comments:
Post a Comment