简体   繁体   中英

Android = TelephonyManager (Intent.ACTION_CALL)

I want to make a call when the TelephonyManager state IDLE detects a true value on CallEnded variable.

The first call goes normally, but when changes from OFFHOOK to IDLE I need a new call intent to be performed, but there is no call showing up.

What I'm missing ?

Thanks for your time.

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private Boolean CallEnded = false;

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

final TelephonyManager telephoneM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

PhoneStateListener listener = new PhoneStateListener() {

        public void onCallStateChanged(int state, String incomingNumber) {


            switch (state) {

                case TelephonyManager.CALL_STATE_IDLE:

                    if(CallEnded){

                     performDial();

                    }


                    break;


                case TelephonyManager.CALL_STATE_RINGING:

                    break;


                case TelephonyManager.CALL_STATE_OFFHOOK:

                  CallEnded=true;                       

            }
        }
    };


    telephoneM.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);


Button button = (Button) findViewById(R.id.button);


button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            performDial();

        }
    });

  }

  private void performDial() {

    Intent dial = new Intent(Intent.ACTION_CALL);
    dial.setData(Uri.parse("tel:911"));

    if (ActivityCompat.checkSelfPermission(MainActivity.this,     Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){

        return;
    }

 }

Manifest

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

First issue is that you're not allowed to ACTION_CALL an emergency number like 911 of you're not a system app. The intent will be converted to an ACTION_DIAL intent, causing the system phone app to appear and allow the user to manually call the number if she chooses to.

I would try first with a standard phone number to make sure this is not the issue.

Second issue is related to calling the same intent twice on Android. In certain cases Android might detect the exact same intent, and will simply ignore the new one. If that might be the issue, try adding some constantly changing value to your url's data, like:

Intent dial = new Intent(Intent.ACTION_CALL);
dial.setData(Uri.parse("tel:212-555-1234" + "?time=" + System.currentTimeMillis()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM