151

我正在尝试制作一个应用程序,例如,当电话打到我想检测号码时。以下是我尝试过的,但它没有检测到来电。

我想MainActivity在后台运行我的,我该怎么做?

manifest我已经在文件中给予了许可。

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

我还应该在清单中提供什么吗?

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
   }

   public class myPhoneStateChangeListener extends PhoneStateListener {
       @Override
       public void onCallStateChanged(int state, String incomingNumber) {
           super.onCallStateChanged(state, incomingNumber);
           if (state == TelephonyManager.CALL_STATE_RINGING) {
               String phoneNumber =   incomingNumber;
           }
       }
   }
}
4

12 回答 12

362

这是我用来执行此操作的:

显现:

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

<!--This part is inside the application-->
    <receiver android:name=".CallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

我的基本可重复使用呼叫检测器

package com.gabesechan.android.reusable.receivers;

import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

public abstract class PhonecallReceiver extends BroadcastReceiver {

    //The receiver will be recreated whenever android feels like it.  We need a static variable to remember data between instantiations

    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;  //because the passed incoming is only valid in ringing


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

        //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
            savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
        }
        else{
            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            int state = 0;
            if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                state = TelephonyManager.CALL_STATE_IDLE;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                state = TelephonyManager.CALL_STATE_OFFHOOK;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                state = TelephonyManager.CALL_STATE_RINGING;
            }


            onCallStateChanged(context, state, number);
        }
    }

    //Derived classes should override these to respond to specific events of interest
    protected abstract void onIncomingCallReceived(Context ctx, String number, Date start);
    protected abstract void onIncomingCallAnswered(Context ctx, String number, Date start);
    protected abstract void onIncomingCallEnded(Context ctx, String number, Date start, Date end);

    protected abstract void onOutgoingCallStarted(Context ctx, String number, Date start);      
    protected abstract void onOutgoingCallEnded(Context ctx, String number, Date start, Date end);

    protected abstract void onMissedCall(Context ctx, String number, Date start);

    //Deals with actual events

    //Incoming call-  goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
    //Outgoing call-  goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
    public void onCallStateChanged(Context context, int state, String number) {
        if(lastState == state){
            //No change, debounce extras
            return;
        }
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                savedNumber = number;
                onIncomingCallReceived(context, number, callStartTime);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //Transition of ringing->offhook are pickups of incoming calls.  Nothing done on them
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    callStartTime = new Date();
                    onOutgoingCallStarted(context, savedNumber, callStartTime);                     
                }
                else
                {
                    isIncoming = true;
                    callStartTime = new Date();
                    onIncomingCallAnswered(context, savedNumber, callStartTime); 
                }

                break;
            case TelephonyManager.CALL_STATE_IDLE:
                //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    //Ring but no pickup-  a miss
                    onMissedCall(context, savedNumber, callStartTime);
                }
                else if(isIncoming){
                    onIncomingCallEnded(context, savedNumber, callStartTime, new Date());                       
                }
                else{
                    onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());                                               
                }
                break;
        }
        lastState = state;
    }
}

然后使用它,只需从它派生一个类并实现一些简单的功能,无论您关心哪种调用类型:

public class CallReceiver extends PhonecallReceiver {

    @Override
    protected void onIncomingCallReceived(Context ctx, String number, Date start)
    {
        //
    }

    @Override
    protected void onIncomingCallAnswered(Context ctx, String number, Date start)
    {
        //
    }

    @Override
    protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end)
    {
        //
    }

    @Override
    protected void onOutgoingCallStarted(Context ctx, String number, Date start)
    {
        //
    } 

    @Override 
    protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end)
    {
        //
    }

    @Override
    protected void onMissedCall(Context ctx, String number, Date start)
    {
        //
    }

}

此外,您可以在我的博客上看到我写的关于代码为何如此的文章。要点链接:https ://gist.github.com/ftvs/e61ccb039f511eb288ee

编辑:更新为更简单的代码,因为我已经重新编写了该类以供自己使用

于 2013-03-22T06:23:32.057 回答
26
private MyPhoneStateListener phoneStateListener = new MyPhoneStateListener();

注册

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

并注销

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
于 2013-03-22T06:21:50.273 回答
19

使用 Android P - Api Level 28: 您需要获得 READ_CALL_LOG 权限

限制访问通话记录

Android P 将CALL_LOGREAD_CALL_LOGWRITE_CALL_LOGPROCESS_OUTGOING_CALLS权限从PHONE权限组移动到新CALL_LOG权限组。该组使用户可以更好地控制和查看需要访问电话敏感信息的应用程序,例如读取电话记录和识别电话号码。

要从 PHONE_STATE 意图操作中读取数字,您需要READ_CALL_LOG权限和READ_PHONE_STATE权限。要从中读取数字onCallStateChanged(),您现在READ_CALL_LOG只需要权限。您不再需要该READ_PHONE_STATE权限。

于 2018-06-13T10:11:01.167 回答
17

更新: Gabe Sechan发布的非常棒的代码不再有效,除非您明确要求用户授予必要的权限。您可以在主要活动中放置一些代码来请求这些权限:

    if (getApplicationContext().checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {
        // Permission has not been granted, therefore prompt the user to grant permission
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_PHONE_STATE},
                MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
    }

    if (getApplicationContext().checkSelfPermission(Manifest.permission.PROCESS_OUTGOING_CALLS)
            != PackageManager.PERMISSION_GRANTED) {
        // Permission has not been granted, therefore prompt the user to grant permission
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS},
                MY_PERMISSIONS_REQUEST_PROCESS_OUTGOING_CALLS);
    }

另外:正如Gabe 帖子下方的评论中提到的那样,您必须向接收器添加一小段代码,android:enabled="true以便在应用程序当前未在前台运行时检测来电:

    <!--This part is inside the application-->
    <receiver android:name=".CallReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>
于 2018-03-01T00:20:36.097 回答
7

只是为了更新 Gabe Sechan 的答案。如果您的清单要求获得 READ_CALL_LOG 和 READ_PHONE_STATE 的权限,onReceive 将调用TWICE。其中一个有 EXTRA_INCOMING_NUMBER,另一个没有。你必须测试哪个有它,它可以以任何顺序发生。

https://developer.android.com/reference/android/telephony/TelephonyManager.html#ACTION_PHONE_STATE_CHANGED

于 2019-06-27T23:02:49.730 回答
5

这可能会对您有所帮助,并添加要求权限

public class PhoneListener extends PhoneStateListener
{
    private Context context;
    public static String getincomno;

    public PhoneListener(Context c) {
        Log.i("CallRecorder", "PhoneListener constructor");
        context = c;
    }

    public void onCallStateChanged (int state, String incomingNumber)
    {

        if(!TextUtils.isEmpty(incomingNumber)){
        // here for Outgoing number make null to get incoming number
        CallBroadcastReceiver.numberToCall = null;
        getincomno = incomingNumber;
        }

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:

            break;
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d("CallRecorder", "CALL_STATE_RINGING");
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:

            break;
        }
    }
}
于 2013-03-22T06:27:19.057 回答
5

我修复了Gabe Sechan的答案,我使用了以下代码并且它工作正常。我注意到当接收者意图具有“ incoming_number ”键时,我可以获得电话号码。所以我过滤了传入的意图并使用EXTRA_INCOMING_NUMBEREXTRA_PHONE_NUMBER来获取电话号码。

public class ChangeCallStateListener extends BroadcastReceiver {

    private static String lastState = TelephonyManager.EXTRA_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;  //because the passed incoming is only valid in ringing

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("CallObserver", "CallReceiver is starting ....");

        List<String> keyList = new ArrayList<>();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            keyList = new ArrayList<>(bundle.keySet());
            Log.e("CallObserver", "keys : " + keyList);
        }

        if (keyList.contains("incoming_number")) {
            String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String phoneIncomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            String phoneOutgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

            String phoneNumber = phoneOutgoingNumber != null ? phoneOutgoingNumber : (phoneIncomingNumber != null ? phoneIncomingNumber : "");

            if (phoneState != null && phoneNumber != null) {
                if (lastState.equals(phoneState)) {
                    //No change, debounce extras
                    return;
                }
                Log.e("CallObserver", "phoneState = " + phoneState);
                if (TelephonyManager.EXTRA_STATE_RINGING.equals(phoneState)) {
                    isIncoming = true;
                    callStartTime = new Date();
                    //
                    lastState = TelephonyManager.EXTRA_STATE_RINGING;
                    if (phoneNumber != null) {
                        savedNumber = phoneNumber;
                    }


                    onIncomingCallStarted(context, savedNumber, callStartTime);
                } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(phoneState)) {

                    if (lastState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        //
                        lastState = TelephonyManager.EXTRA_STATE_IDLE;
                        onMissedCall(context, savedNumber, callStartTime);
                    } else {
                        if (isIncoming) {
                            //
                            lastState = TelephonyManager.EXTRA_STATE_IDLE;
                            onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
                        } else {
                            //
                            lastState = TelephonyManager.EXTRA_STATE_IDLE;
                            Log.d("CallObserver", "onOutgoingCallEnded called !! : ");
                            onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
                        }

                    }
                } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(phoneState)) {
                    if (lastState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        isIncoming = true;
                    } else {
                        isIncoming = false;
                    }
                    callStartTime = new Date();
                    savedNumber = phoneNumber;
                    //
                    lastState = TelephonyManager.EXTRA_STATE_OFFHOOK;
                    onOutgoingCallStarted(context, savedNumber, callStartTime);
                }
            }
        }

    }


    protected void onIncomingCallStarted(Context ctx, String number, Date start) {
        Log.d("CallObserver", "onIncomingCallStarted  :  " + " number is  : " + number);
    }

    protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
        Log.d("CallObserver", "onOutgoingCallStarted  :  " + " number is  : " + number);
    }

    protected void onIncomingCallEnded(Context context, String number, Date start, Date end) {
    }

    protected void onOutgoingCallEnded(Context context , String number, Date start, Date end) {
    }

    protected void onMissedCall(Context context, String number, Date start) {
    }

}
  • 不要忘记获得运行时许可。

      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    
于 2021-06-22T04:27:36.727 回答
3

这是一个简单的方法,可以避免使用PhonestateListener和其他并发症。
所以在这里我们接收到来自 android 的 3 个事件,例如RINGING,OFFHOOKIDLE. 为了获得所有可能的调用状态,我们需要定义我们自己的状态,如RINGING, OFFHOOK, IDLE, FIRST_CALL_RINGING, SECOND_CALL_RINGING。它可以处理电话中的每个状态。
请以我们正在接收来自 android 的事件的方式思考,我们将定义我们的 on call 状态。查看代码。

public class CallListening  extends BroadcastReceiver {
    private static final String TAG ="broadcast_intent";
    public static String incoming_number;
    private String current_state,previus_state,event;
    public static Boolean dialog= false;
    private Context context;
    private SharedPreferences sp,sp1;
    private SharedPreferences.Editor spEditor,spEditor1;
    public void onReceive(Context context, Intent intent) {
        //Log.d("intent_log", "Intent" + intent);
        dialog=true;
        this.context = context;
        event = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        incoming_number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.d(TAG, "The received event : "+event+", incoming_number : " + incoming_number);
        previus_state = getCallState(context);
        current_state = "IDLE";
        if(incoming_number!=null){
            updateIncomingNumber(incoming_number,context);
        }else {
            incoming_number=getIncomingNumber(context);
        }
        switch (event) {
            case "RINGING":
                Log.d(TAG, "State : Ringing, incoming_number : " + incoming_number);
            if((previus_state.equals("IDLE")) || (previus_state.equals("FIRST_CALL_RINGING"))){
                    current_state ="FIRST_CALL_RINGING";
                }
                if((previus_state.equals("OFFHOOK"))||(previus_state.equals("SECOND_CALL_RINGING"))){
                    current_state = "SECOND_CALL_RINGING";
                }

                break;
            case "OFFHOOK":
                Log.d(TAG, "State : offhook, incoming_number : " + incoming_number);
                if((previus_state.equals("IDLE")) ||(previus_state.equals("FIRST_CALL_RINGING")) || previus_state.equals("OFFHOOK")){
                    current_state = "OFFHOOK";
                }
                if(previus_state.equals("SECOND_CALL_RINGING")){
                    current_state ="OFFHOOK";
                    startDialog(context);
                }
                break;
            case "IDLE":
                Log.d(TAG, "State : idle and  incoming_number : " + incoming_number);
                if((previus_state.equals("OFFHOOK")) || (previus_state.equals("SECOND_CALL_RINGING")) || (previus_state.equals("IDLE"))){
                    current_state="IDLE";
                }
                if(previus_state.equals("FIRST_CALL_RINGING")){
                    current_state = "IDLE";
                    startDialog(context);
                }
                updateIncomingNumber("no_number",context);
                Log.d(TAG,"stored incoming number flushed");
                break;
        }
        if(!current_state.equals(previus_state)){
            Log.d(TAG, "Updating  state from "+previus_state +" to "+current_state);
            updateCallState(current_state,context);

        }
    }
    public void startDialog(Context context) {
        Log.d(TAG,"Starting Dialog box");
        Intent intent1 = new Intent(context, NotifyHangup.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent1);

    }
    public void updateCallState(String state,Context context){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        spEditor = sp.edit();
        spEditor.putString("call_state", state);
        spEditor.commit();
        Log.d(TAG, "state updated");

    }
    public void updateIncomingNumber(String inc_num,Context context){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        spEditor = sp.edit();
        spEditor.putString("inc_num", inc_num);
        spEditor.commit();
        Log.d(TAG, "incoming number updated");
    }
    public String getCallState(Context context){
        sp1 = PreferenceManager.getDefaultSharedPreferences(context);
        String st =sp1.getString("call_state", "IDLE");
        Log.d(TAG,"get previous state as :"+st);
        return st;
    }
    public String getIncomingNumber(Context context){
        sp1 = PreferenceManager.getDefaultSharedPreferences(context);
        String st =sp1.getString("inc_num", "no_num");
        Log.d(TAG,"get incoming number as :"+st);
        return st;
    }
}
于 2015-10-28T11:58:26.237 回答
1

@Gabe Sechan,感谢您的代码。它工作正常,除了onOutgoingCallEnded(). 它永远不会被执行。测试手机是三星 S5 & Trendy。我认为有2个错误。

1:缺少一对括号。

case TelephonyManager.CALL_STATE_IDLE: 
    // Went to idle-  this is the end of a call.  What type depends on previous state(s)
    if (lastState == TelephonyManager.CALL_STATE_RINGING) {
        // Ring but no pickup-  a miss
        onMissedCall(context, savedNumber, callStartTime);
    } else {
        // this one is missing
        if(isIncoming){
            onIncomingCallEnded(context, savedNumber, callStartTime, new Date());                       
        } else {
            onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());                                               
        }
    }
    // this one is missing
    break;

2:如果在函数末尾,lastState则不更新。state它应该被替换为这个函数的第一行

public void onCallStateChanged(Context context, int state, String number) {
    int lastStateTemp = lastState;
    lastState = state;
    // todo replace all the "lastState" by lastStateTemp from here.
    if (lastStateTemp  == state) {
        //No change, debounce extras
        return;
    }
    //....
}

另外,我已经按照您lastStatesavedNumber建议加入了共享偏好。

刚刚使用上述更改对其进行了测试。至少在我的手机上修复了错误。

于 2015-10-22T21:04:12.370 回答
1

请使用以下代码。它将帮助您获取带有其他呼叫详细信息的来电号码。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/call"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

private static final int MISSED_CALL_TYPE = 0;
private TextView txtcall;

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

    txtcall = (TextView) findViewById(R.id.call);

    StringBuffer sb = new StringBuffer();
    Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
            null, null, null);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    sb.append("Call Details :");
    while (managedCursor.moveToNext()) {
        String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = managedCursor.getString(duration);
        String dir = null;
        int dircode = Integer.parseInt(callType);
        switch (dircode) {

        case CallLog.Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;

        case CallLog.Calls.INCOMING_TYPE:
            dir = "INCOMING";
            break;

        case CallLog.Calls.MISSED_TYPE:
            dir = "MISSED";
            break;
        }
        sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
                + dir + " \nCall Date:--- " + callDayTime
                + " \nCall duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");
    }
    managedCursor.close();
    txtcall.setText(sb);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

} 

并在您的清单请求中获得以下权限:

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
于 2019-01-23T03:22:01.320 回答
1

您需要一个 BroadcastReceiver ,ACTION_PHONE_STATE_CHANGED这将在电话状态从空闲、响铃、摘机改变时调用您的接收者,以便从以前的值和您可以检测到的新值是否是传入/传出呼叫。

所需的许可是:

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

但是,如果您还想在该广播中接收 EXTRA_INCOMING_NUMBER,则需要另一个权限:“android.permission.READ_CALL_LOG”

代码是这样的:

val receiver: BroadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Log.d(TAG, "onReceive")
    }
}

override fun onResume() {
    val filter = IntentFilter()
    filter.addAction("android.intent.action.PHONE_STATE")
    registerReceiver(receiver, filter)
    super.onResume()
}

override fun onPause() {
    unregisterReceiver(receiver)
    super.onPause()
}

在接收器类中,我们可以通过读取这样的意图来获取当前状态:

intent.extras["state"]

extras 的结果可能是:

RINGING -> 如果您的手机正在响铃

摘机 -> 如果您正在与某人通话(来电或去电)

IDLE -> 如果通话结束(呼入或呼出)

使用 PHONE_STATE 广播,我们不需要使用 PROCESS_OUTGOING_CALLS 权限或已弃用的 NEW_OUTGOING_CALL 操作。

于 2020-07-01T06:03:41.760 回答
0

请参阅Gabe Sechan的答案。如前所述,在拨出呼叫的情况下,我们有以下状态更改:IDLE -> OFFHOOK -> IDLE。在 Gabe 的原始答案中,savedNumber仅当电话状态变为 RINGING 时才设置,这对于拨出电话不是真的。savedNumber当电话状态变为 OFFHOOK 时也设置的一个小修复:

case TelephonyManager.CALL_STATE_OFFHOOK:
    if(lastState != TelephonyManager.CALL_STATE_RINGING){
        //IDLE to OFFHOOK for example.
        isIncoming = false;
        callStartTime = new Date();
        savedNumber = number;
        onOutgoingCallStarted(context, savedNumber, callStartTime);
    }
...

此修复程序允许将拨打的号码传递给拨出呼叫方法,就像传入号码传递给传入呼叫或未接呼叫方法一样。

于 2022-01-24T13:40:03.027 回答