3

I have an app that listens for incoming calls and depending upon the number sends an SMS to that number. Everything is working on incoming the onReceive is called and SMS is sent but as soon as the call is rejected either by called or caller party onReceive is called again and SMS is sent again. How can I limit this, so when a call is received only then the onReceive is called?

Following is the simple version of my code:

My BroadcastReceiver

import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log;

public class Telephone extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        Log.d("onReceive", "Got Call Signal");

    }

}

in Manifest:

         <receiver android:name="com.example.testbroadcastreceiver.Telephone" >
            <intent-filter android:priority="1000" >
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

Permission used:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Atif Farrukh
  • 2,219
  • 2
  • 25
  • 47

2 Answers2

0

You can handle it logically like this:

public class ServiceReceiver extends BroadcastReceiver {

  @Override    
  public void onReceive(Context context, Intent intent) {
    MyPhoneStateListener phoneListener=new MyPhoneStateListener();
    TelephonyManager telephony = (TelephonyManager) 
    context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
  }
}

public class MyPhoneStateListener extends PhoneStateListener {
  public void onCallStateChanged(int state,String incomingNumber){
  switch(state){
    case TelephonyManager.CALL_STATE_IDLE:
      Log.d("DEBUG", "IDLE");
    break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
      Log.d("DEBUG", "OFFHOOK");
    break;
    case TelephonyManager.CALL_STATE_RINGING:
      Log.d("DEBUG", "RINGING");
//place your sms code here
    break;
    }
  } 
}
Trinimon
  • 13,839
  • 9
  • 44
  • 60
curious
  • 371
  • 1
  • 9
0

You will need to register an PhoneStateListener.LISTEN_CALL_STATE for receiving only incoming call inside BroadcastReceiver as:

public class Telephone extends BroadcastReceiver {

    private Context mContext;
    private Intent mIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        mIntent = intent;
        TelephonyManager tm = (TelephonyManager)              
                 context.getSystemService(Context.TELEPHONY_SERVICE);
        int events = PhoneStateListener.LISTEN_CALL_STATE;
        tm.listen(phoneStateListener, events);
    }

    private final PhoneStateListener phoneStateListener = 
                                                new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            String callState = "UNKNOWN";
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:

                break;
            case TelephonyManager.CALL_STATE_RINGING:
                                // DO YOUR WORK HERE
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                          if(incomingNumber.length() >0){

                                  // incoming call
                                 // DO YOUR WORK HERE when call is received
                                 // SEND SMS HERE
                            }
                           else{
                                      // outgoing call
                              }
                break;
            }

            super.onCallStateChanged(state, incomingNumber);
        }
    };

}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213