0

I use this code to send a sms to the incoming number before receiving this call, but it does not send sms. In manifext file I add these permission

 <uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>

public class check extends BroadcastReceiver {


Context context = null;
 private static final String TAG = "Phone call";
 private ITelephony telephonyService;


    @Override
     public void onReceive(Context context, Intent intent) {
      Log.v(TAG, "Receving....");

      TelephonyManager telephony = (TelephonyManager) 
      context.getSystemService(Context.TELEPHONY_SERVICE);  
      try {
       Class c = Class.forName(telephony.getClass().getName());
       Method m = c.getDeclaredMethod("getITelephony");
       m.setAccessible(true);
       Bundle b=intent.getExtras();
       num=b.getString(telephony.EXTRA_INCOMING_NUMBER);
       String g=num.substring(num.length()-11,num.length());

       SmsManager smsManager = SmsManager.getDefault();
             smsManager.sendTextMessage(num,null, "rrrrrrrrr", null,null);


      } catch (Exception e) {
       e.printStackTrace();
      }

     }

please anyone call help me

  • How is it not working? No text sent? Some error? – keyser Jan 24 '13 at 14:15
  • Do you have the proper permissions defined in the manifest? Also "send a sms to the incoming number before receiving this call" You first have to be receiveing the call for there to be an extra in the intent. – A--C Jan 24 '13 at 15:10
  • 1
    Also consider using a PhoneStateListener, as talked about [here](http://stackoverflow.com/questions/1853220/retrieve-incoming-calls-phone-number-in-android) rather than using reflection. – A--C Jan 24 '13 at 15:16

1 Answers1

0

Make your class extend PhoneStateListener then use this method.

public void onCallStateChanged(int state, String phoneNumber) {
    SmsManager sms = SmsManager.getDefault();
    String message = "";
    if (state == TelephonyManager.CALL_STATE_RINGING) {
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
}
user1959417
  • 224
  • 2
  • 8