TelephonyManager 在 android 12 上弃用了 listen() CALL_STATE_RINGING

TelephonyManager deprecated listen() CALL_STATE_RINGING on android 12

如果我的应用程序在前台运行时有 phone 呼叫,我想听听。

以前是这样,但现在不推荐使用 listen():

val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            tm.listen(object : PhoneStateListener() {
                override fun onCallStateChanged(state: Int, phoneNumber: String?) {
                    super.onCallStateChanged(state, phoneNumber)
                    when (state) {
                        TelephonyManager.CALL_STATE_RINGING -> transcribingAudioConsumer.stopTranscription(null)
                        else -> {}
                    }
                }
            }, PhoneStateListener.LISTEN_CALL_STATE)

我尝试过类似的方法,但找不到正确的实现方法。

         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                        tm.registerTelephonyCallback({ it.run() }, MyPhoneStateListener())
                    }
  @RequiresApi(Build.VERSION_CODES.S)
    class MyPhoneStateListener : TelephonyCallback(), TelephonyCallback.CallStateListener {
        override fun onCallStateChanged(state: Int) {
            when (state) {
                TelephonyManager.CALL_STATE_RINGING -> {
                    Timber.e("omg RING")
                }

                TelephonyManager.CALL_STATE_OFFHOOK -> {
                    Timber.e("omg hook")
                }
                TelephonyManager.CALL_STATE_IDLE -> {
                    Timber.e("omg idle")
                }
            }
        }
    }

我使用了你所做的,android 12 模拟器也 worked.I 将它用于低于 android 12 的版本,我希望它能工作。

    class MyPhoneStateListener() : PhoneStateListener() {
    override fun onCallStateChanged(state: Int, incomingNumber: String) {
        when (state) {
            TelephonyManager.CALL_STATE_IDLE -> {
                Log.e("MyPhoneStateListener", "IDLE")
            
            }
            TelephonyManager.CALL_STATE_OFFHOOK -> {
                Log.e("MyPhoneStateListener", "OFFHOOK")
            }
            TelephonyManager.CALL_STATE_RINGING -> {
                Log.e("MyPhoneStateListener", "RINGING")
            }
        }
    }


}

我用这个来听电话


 phoneListener = MyPhoneStateListener()
             var telephony: TelephonyManager? = null
                telephony = context
                    .getSystemService(TELEPHONY_SERVICE) as TelephonyManager?

                telephony!!.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE)

最后你应该像这里一样制作一个 ServiceReceiver class。 Add PhoneStateListener

Android 12 :我在这里找到了一个例子。我希望这个对你有用。 https://note.com/koh_49/n/n94a5c2ae3aa2

  @RequiresApi(Build.VERSION_CODES.S)
class CustomTelephonyCallback(private val func: (state: Int) -> Unit) :
TelephonyCallback(),
TelephonyCallback.CallStateListener {
    override fun onCallStateChanged(state: Int) {
        func(state)
    }
}
@RequiresApi(Build.VERSION_CODES.S)
fun registerCustomTelephonyCallback(){

     var callback: CustomTelephonyCallback? = null


        (getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager)?.
        registerTelephonyCallback(
            mainExecutor,
            CustomTelephonyCallback {
                //
                var state=it
             

            }.also {
                callback = it
            }
        )


}

  

清单

    <receiver android:name=".data.service.receiver.ServiceReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

服务接收者

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat.getMainExecutor


class ServiceReceiver: BroadcastReceiver() {
@RequiresApi(Build.VERSION_CODES.S)
override fun onReceive(context: Context?, intent: Intent?) {

    registerCustomTelephonyCallback(context)


}

@RequiresApi(Build.VERSION_CODES.S)
class CustomTelephonyCallback(private val func: (state: Int) -> Unit) :
    TelephonyCallback(),
    TelephonyCallback.CallStateListener {
    override fun onCallStateChanged(state: Int) {
        func(state)
    }
}

@RequiresApi(Build.VERSION_CODES.S)
fun registerCustomTelephonyCallback(context: Context?){

    var callback: CustomTelephonyCallback? = null

    (context?.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager)?.
    registerTelephonyCallback(
        getMainExecutor(context),
        CustomTelephonyCallback {
            //
            var state=it


        }.also {
            callback = it
        }
    )


}

}

服务接收者(Java)

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;

public class ServiceReceiver extends BroadcastReceiver {

@RequiresApi(api = Build.VERSION_CODES.S)
@Override
public void onReceive(Context context, Intent intent) {
    registerCustomTelephonyCallback(context);
}

@RequiresApi(Build.VERSION_CODES.S)
class CustomTelephonyCallback extends TelephonyCallback implements TelephonyCallback.CallStateListener {
    private CallBack mCallBack;

    public CustomTelephonyCallback(CallBack callBack) {
        mCallBack = callBack;
    }

    @Override
    public void onCallStateChanged(int state) {

        mCallBack.callStateChanged(state);

    }
}


@RequiresApi(Build.VERSION_CODES.S)
public void registerCustomTelephonyCallback(Context context) {
    TelephonyManager telephony = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

    telephony.registerTelephonyCallback(context.getMainExecutor(), new CustomTelephonyCallback(new CallBack() {
        @Override
        public void callStateChanged(int state) {

            int myState=state;

        }
    }));


}

interface CallBack {
    void callStateChanged(int state);
}

}

自从 api 31 android 12 起不推荐使用 listen 方法后,我做了一个简单的方法来收听电话回调。

val telephonyManager: TelephonyManager =
    context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    telephonyManager.registerTelephonyCallback(
        context.mainExecutor,
        object : TelephonyCallback(), TelephonyCallback.CallStateListener {
            override fun onCallStateChanged(state: Int) {
            }
        })
} else {
    telephonyManager.listen(object : PhoneStateListener() {
        override fun onCallStateChanged(state: Int, phoneNumber: String?) {
        }
    }, PhoneStateListener.LISTEN_CALL_STATE)
}

请注意,新回调不包含 phone 号码。 至少对于广播接收器,可以通过

检索 phone 号码
intent.extras.getString("incoming_number")