Open your Android Studio and click on new project.
Give it a Name.
Select Your Minimum SDK version.
Select a Blank Activity.
And Then Hit Next Next, and Then click on Finish.
Now Here is your Sending Part(java code).
public void sendMessage(View v) // this method will an argument of type View
// when the Send Button is pressed in keyboard.
{
String number= numberEdit.getText().toString(); //Getting number From phone
// EditText
String message=messageEdit.getText().toString(); //Getting Message from
//message EditText
if(number.length()>0&&message.length()>0)
{
try {
smsManager.sendTextMessage(number, null, message, null, null); //Send SMS
Toast.makeText(this, "SMS send successfully", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
//Throw Exception in LogCat if message sending Failed for some Reason
Toast.makeText(this, "SMS sending Failed", Toast.LENGTH_SHORT).show(); e.printStackTrace();
Log.i("Wisal","OOOpsssss Error While Receiving SMS",e); }
} // if end
else // Only show when you left either phone number or message editText empty.
{
Toast toast=Toast.makeText(this,"Please fill the Required Field"
,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0); toast.show();
}
}
And here is Receiving part (java code).
smsReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Telephony.Sms.Intents.DATA_SMS_RECEIVED_ACTION));
receiveText.setText("");
Bundle bundle=intent.getExtras(); //getting data from the intent
try{
if(bundle!=null)
{
String message=null; //initialized the String,to display the resulted text in text view
final Object[] pdu= (Object[]) bundle.get("pdus"); //create an array of pdu to store the incoming text
// pdus is the industrial format for the SMS OOhhhh.
for(int i=0;i<pdu.length;i++)
{
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdu[i]);
String phoneNumber=currentMessage.getDisplayOriginatingAddress(); //get the Phone number from the Received SMs
message = currentMessage.getDisplayMessageBody();
// To Show you the phone number and SMS in the Toast
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(getApplicationContext(),"Phone Number : "
+phoneNumber +"\n"+"Message : "+ message, duration);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
} // for loop end
receiveText.setText(message); // To show the SMS in Text View
} // Bundle null
} // try End
catch(Exception e)
{
Log.i("Wisal","Error While Receiving SMS",e); //Printing the Exception in LogCat
} //catch End.
} //on Receive End
}; //Broadcast Receiver End
Don't forget to add permission in manifest file.... :)
Great ...
ReplyDeleteThankssss <3
DeleteGood work, keep it up.
ReplyDelete