mercredi 25 janvier 2017

How can I use Broadcastreceiver in Androidstudio?

I would like to make make two calls in a row in an android app. Upon clicking a button the app should call the first number and after that it should sense the the first call have just ended (should not matter which party hook up) and call automatically the second number. I've learned that it is possible to detect that a call ended with the class that I placed below the class MainActivity. (class PhoneStateBroadcastReceiver ). Still I do not really understand what should I know write into my main class. I guess, I should write something between the two lines where I call the calling method to get the state of the phone, right?

package com.example.bedaa.drivecaller;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;

 public class MainActivity extends AppCompatActivity {



private void calling(String phone) {
    Intent callIntent = new Intent(Intent.ACTION_CALL)
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    callIntent.setData(Uri.parse("tel:" + phone));
    callIntent.putExtra("com.android.phone.extra.slot", 1);
    startActivity(callIntent);
}




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




      Button b = (Button) this.findViewById(R.id.CallButton);


     b.setOnClickListener(new View.OnClickListener() {
      @Override
    public void onClick(View v) {
          calling("+36309577686");
          calling("+36303853440");


        }
      });

   }
 }

 class PhoneStateBroadcastReceiver extends BroadcastReceiver {

Context mContext;
String incoming_nr;
private int prev_state;

@Override
public void onReceive(Context context, Intent intent) {
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
    CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager
    mContext = context;
}
/* Custom PhoneStateListener */
public class CustomPhoneStateListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (incomingNumber != null && incomingNumber.length() > 0) incoming_nr = incomingNumber;

        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                prev_state = state;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                prev_state = state;
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if ((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)) {
                    prev_state = state;
                    Toast.makeText(mContext, "Call End", Toast.LENGTH_SHORT).show();
                    //Answered Call which is ended
                }
                if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) {
                    prev_state = state;
                    //Rejected or Missed call
                }
                break;

        }
    }
}

}

Aucun commentaire:

Enregistrer un commentaire