0

In android how should i get phone number of sms sender?

I make application which sends sms but takes money charges for that, so can i send sms without charging money? please tell me

Angel
  • 307
  • 3
  • 4
  • 9
  • no body knows answer of this?please reply if anybody knows ? – Angel Apr 17 '11 at 08:53
  • Rahul, i make app which sends sms,but when i sends sms it takes money chrges to send sms.so instead charging money to send sms can i send free sms? also i want to get the phone number from which i recieved sms. – Angel Apr 17 '11 at 09:02
  • Two questions in one. You should ask separately. – Francisco R May 29 '13 at 11:26

3 Answers3

8

To send SMS without Spending Money is only possible if you implement any FREE SMS GATEWAY. Based on what country you are, you will find any FREE SMS GATEWAY and try to find any web services or API they are providing. Write a code using that and you will be able to send SMS for FREE. Make sure this required an internet connection on your phone.

If you implement a BroadCast Receiver for Incoming SMS in that case following is the code which will track your imcoming SMS and will give you the Message and Sender Number.

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

public class SmsReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        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]);                
                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";        
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }                         
    }
}
Rahul Patel
  • 1,198
  • 7
  • 5
  • HI rahul actaullay **i want to extract only the phone number of incoming sms and want to display that phone number in textview**.the code which u send me , i alredy wrote to send sms.so plese tell if you know another – Angel Apr 17 '11 at 09:46
1

the method .getOriginatingAddress() in rahul code gives the senders phone no. so use it wherever you want.simple!!

MVijayvargia
  • 331
  • 2
  • 10
0

Here's an awesome reference. It has the following tutorial link to the code below.

It worked great for me!

public class SmsReceiver extends BroadcastReceiver {

@Override
  public void onReceive(Context context, Intent intent) {
        final String tag = TAG + ".onReceive";
        Bundle bundle = intent.getExtras();
        if (bundle == null) {
              Log.w(tag, "BroadcastReceiver failed, no intent data to process.");
              return;
        }
        if (intent.getAction().equals(SMS_RECEIVED)) {
              Log.d(tag, "SMS_RECEIVED");

              String smsOriginatingAddress, smsDisplayMessage;

               /**
               * You have to CHOOSE which code snippet to use NEW (KitKat+), or legacy
               * Please comment out the for{} you don't want to use.
               */
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                 // API level 19 (KitKat 4.4) getMessagesFromIntent
                 for (SmsMessage message : Telephony.Sms.Intents.
                                        getMessagesFromIntent(intent)) {
                    Log.d(tag, "KitKat or newer");
                    if (message == null) {
                          Log.e(tag, "SMS message is null -- ABORT");
                          break;
                    }
                    smsOriginatingAddress = message.getDisplayOriginatingAddress();
                    //see getMessageBody();
                    smsDisplayMessage = message.getDisplayMessageBody();
                    processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
                  }
               } else {

                  // Processing SMS messages the OLD way, before KitKat,
                  // this WILL work on KitKat or newer Android
                  // PDU is a “protocol data unit”, which is the industry
                  // format for an SMS message
                  Object[] data = (Object[]) bundle.get("pdus");
                  for (Object pdu : data) {
                    Log.d(tag, "legacy SMS implementation (before KitKat)");
                    SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
                    if (message == null) {
                          Log.e(tag, "SMS message is null -- ABORT");
                          break;
                    }
                    smsOriginatingAddress = message.getDisplayOriginatingAddress();
                    // see getMessageBody();
                    smsDisplayMessage = message.getDisplayMessageBody();
                    processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
                  }
               }
        } // onReceive method
Community
  • 1
  • 1
craned
  • 2,991
  • 2
  • 34
  • 38