我创建了一个类来检测呼叫是否结束。如果调用结束,如何在main方法中获取布尔值?

时间:2017-01-25 14:10:47

标签: android if-statement broadcastreceiver phone-state-listener

我想在Android应用中连续两次调用。点击一个按钮,应用程序应该拨打第一个号码,之后它应该感觉到第一个呼叫刚刚结束(无论哪一方挂机),并自动呼叫第二个号码。我已经了解到有可能检测到调用以我放在类MainActivity下面的类结束。 (类PhoneStateBroadcastReceiver)。我仍然不明白我应该怎么知道写入我的主课程。我想,我应该在两行之间写一些东西,我调用调用方法来获取手机的状态,对吗?

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("11111111");
          calling("22222222");


        }
      });

   }
 }

 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;

        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

点击按钮,您应拨打第一个号码:

public void onClick(View v) {
      calling("+11111111111");
}

然后你应该在调用第二个号码之前等待BroadcastReceiver的结果:

class PhoneStateBroadcastReceiver extends BroadcastReceiver {

    private Context mContext;
    private CustomPhoneStateListener mPhoneListener;
    private String incoming_nr;
    private int prev_state;

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

        if (mPhoneListener == null) {
            mPhoneListener = new CustomPhoneStateListener();

            // TelephonyManager object
            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);


            // Register our listener with TelephonyManager
            telephony.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    /* Custom PhoneStateListener */
        class CustomPhoneStateListener extends PhoneStateListener {

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

            if (!TextUtils.isEmpty(incomingNumber)) {
                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)) {
                        // A call has now ended
                        Toast.makeText(mContext, "Call End", Toast.LENGTH_SHORT).show();
                        calling("+22222222222");
                        prev_state = state;
                    }
                    else if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) {
                        // Rejected or Missed call
                        prev_state = state;
                    }
                    break;

            }
        }
    }
}

============================================

确保包含在您的清单中:

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

也许可以将“调用”方法更改为以下内容:

private void calling(String number) {
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
    startActivity(intent);
}