2

I'm making an app that will trigger outgoing call with alarm manager. Also, that outgoing call should be monitored, to check if call was successful, busy or whatever. My problem is how to call for PhoneStateListener in BroadCastReceiver?

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startAlert(View view) {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
    }
}

BroadCastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        PackageManager pm = context.getPackageManager();
        int hasPerm = pm.checkPermission(android.Manifest.permission.PROCESS_OUTGOING_CALLS,context.getPackageName());
        if (hasPerm != PackageManager.PERMISSION_GRANTED) {
            Intent myIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+38163670832"));;
            myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }
    }
}

Example of PhoneStateListener that I would like to apply

public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);
    switch (state) {
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //Kada se korisnik javi
            Toast.makeText(context, "Veza uspostavljena", Toast.LENGTH_LONG).show();
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            //Idle stanje
            Toast.makeText(context, "Idle mod", Toast.LENGTH_LONG).show();
            break;
    }
}
Aleksandar Panic
  • 165
  • 1
  • 2
  • 14
  • 1
    Hi, please check this: [Add PhoneStateListener](http://stackoverflow.com/questions/13395633/add-phonestatelistener?answertab=active#tab-top) – Vietnt134 Sep 27 '16 at 08:12
  • Problem is that I'm already making a call in onReceive in BroadCastReceiver. I'm not sure how LISTEN_CALL_STATE will act in this situation. – Aleksandar Panic Sep 27 '16 at 08:21

2 Answers2

3

how to call for PhoneStateListener in BroadCastReceiver?

Hmm... for what? BroadCastReceiver itself can give you all necessary information about current phone state, so you don't need anything else

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        /* Incoming call */
        if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
        } else

        /* Outgoing call */
        if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
        } else

        /* Call ended */
        if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
        }
    }

}
Hahaido
  • 83
  • 1
  • 11
0

Try like below example...

public class PhoneStatReceiver extends BroadcastReceiver{        

        private static final String TAG = "PhoneStatReceiver"; 

        private static boolean incomingFlag = false;

        private static String incoming_number = null;



        @Override

        public void onReceive(Context context, Intent intent) {


                if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){                        

                        incomingFlag = false;

                        String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);        

                        Log.i(TAG, "call OUT:"+phoneNumber);                        

                }else{                        


                        TelephonyManager tm = 

                            (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                        



                        switch (tm.getCallState()) {

                        case TelephonyManager.CALL_STATE_RINGING:

                                incomingFlag = true;

                                incoming_number = intent.getStringExtra("incoming_number");

                                Log.i(TAG, "RINGING :"+ incoming_number);

                                break;

                        case TelephonyManager.CALL_STATE_OFFHOOK:                                

                                if(incomingFlag){

                                        Log.i(TAG, "incoming ACCEPT :"+ incoming_number);

                                }

                                break;



                        case TelephonyManager.CALL_STATE_IDLE:                                

                                if(incomingFlag){

                                        Log.i(TAG, "incoming IDLE");                                

                                }

                                break;

                        } 

                }

        }

}

Register it in your AndroidManifest.xml.

<receiver android:name=".PhoneStatReceiver">  

            <intent-filter>

                 <action android:name="android.intent.action.PHONE_STATE"/>           

                 <action android:name="android.intent.action.NEW_OUTGOING_CALL" />

            </intent-filter>

</receiver>

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

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • Purpose of app is not to monitor incoming call, it should monitor outgoing call. I added CustomPhoneStateListener phoneListener = new CustomPhoneStateListener(); similar to your example but the status is always IDLE, even when the B side answers the call. I guess that's because PhoneStateListener is triggered only in BroadCastReceiver and is not working all the time. – Aleksandar Panic Sep 27 '16 at 09:06
  • have you updated `AndroidManifest.xml` as well for the action `` ? – Priyank Patel Sep 27 '16 at 09:11
  • Yes I did. It like that from beginning. – Aleksandar Panic Sep 27 '16 at 09:14
  • Now it looks its working, I made some changes. Maybe you have an idea how to make a difference between when B side phone rings and when B side answers the call? – Aleksandar Panic Sep 27 '16 at 09:17
  • You can check it by storing previous(last) call state. If previous call state is `OUTGOING` and current call state is `OFFHOOK` then B side answer the call. If previous call state is `RINGING` and current call state is `OFFHOOK` then A side answer the call. – Priyank Patel Sep 27 '16 at 09:26