It is pretty easy to play with received sms on Android, i will go step by step with code example of project.
step 1:
Create a android project and add one more class in project, let's assume for this example "MySMSReceiver.java", and extends this class with
"BroadcastReceiver" there you will get one method "
onReceive()", this method will active when you will receive any sms in this case, so you have to over-ride this method.
package imran.smsreceiver;
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 MySMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in the intent,---
Bundle _bundle = intent.getExtras();
Toast.makeText(context, "A message came", Toast.LENGTH_SHORT).show();
SmsMessage[] _msg = null;
String str = "";
if (_bundle != null)
{
//---retrieve the SMS message received by pdus---
Object[] _pdus = (Object[]) _bundle.get("pdus");
_msg = new SmsMessage[_pdus.length];
for (int i=0; i<_msg.length; i++){
_msg[i] = SmsMessage.createFromPdu((byte[])_pdus[i]);
//---to get address of sender---
str += "SMS from " + _msg[i].getOriginatingAddress();
str += " :";
//---to find message of sender---
str += _msg[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message with sender information.---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}//end of class
The above code will describe you how to get sms sender information as well as sms body.
step 2:
In Manifest.xml you have to add
<receiver>
so that MySMSReceiver class can be active when sms will come on you device.below code of Manifest.xml will describe how to add
<receiver>.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="imran.smsreceiver"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainAcitity"
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=".MySMSReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<!-- PERMISSION -->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>
you have to add permission to received sms in your project as in code above.
run the project and and test it , it will work for you.
Thanks for time.
Imran Ali
Nice....great post for naive to pro.
ReplyDeleteThis blog is really helpful regarding all educational knowledge I earned. It covered a great area of subject which can assist a lot of needy people. Everything mentioned here is clear and very useful.
ReplyDeleteemail Validation
Professionally written blogs are rare to find, however I appreciate all the points mentioned here. I also want to include some other writing skills which everyone must aware of.
ReplyDeleteUse our valid email checker service to weed out Disposable / Temporary email addresses, spam emails
check valid email address