android应用无法检测到电话是否已结束

时间:2019-02-27 19:06:25

标签: android ibm-watson phone-state-listener

您好,我正在尝试创建一个应用,其中在通话结束时它将执行某些操作,但是到目前为止,我已经尝试了电话状态侦听器并且无法注册。我可以在通话开始时启动Java类,但不能在通话结束时启动。基本上,我试图用挂断电话代替onclick操作。

我的onClickListener:

 mic.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {

    if (!listening) {
       makePhoneCall();
      // Update the icon background
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          mic.setBackgroundColor(Color.GREEN);
        }
      });




    } else {
      // Update the icon background
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mic.setBackgroundColor(Color.LTGRAY);
            }
        });

        microphoneHelper.closeInputStream();
        listening = false;
    }
  }
});

我的makePhoneCall:

public void makePhoneCall(){
    String number=input.getText().toString();
    if (number.trim().length()>0)
    {
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL);
        }
        else{
            String dial="tel:"+number;
            startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
            capture = microphoneHelper.getInputStream(true);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {

                        speechService.recognizeUsingWebSocket(getRecognizeOptions(capture),
                                new MicrophoneRecognizeDelegate());
                    } catch (Exception e) {
                        showError(e);
                    }
                }
            }).start();
            listening = true;
        }
    }
    else
    {
        Toast.makeText(MainActivity.this, "Enter Phone Number", Toast.LENGTH_SHORT).show();
    }
}

我的PhoneStateListener:

public class CallReciever extends BroadcastReceiver {

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

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {

            // Phone number
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

            // Ringing state
            // This code will execute when the phone has an incoming call
        } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)
                ) {

            // This code will execute when the call is answered
        }else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_OFFHOOK)){
            microphoneHelper.closeInputStream();
            listening = false;

        }

    }
}

0 个答案:

没有答案