4

我想听听当我的应用程序在前台时是否有电话。

以前是这样的,但现在不推荐使用 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")
                }
            }
        }
    }
4

3 回答 3

3

由于自 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)
}

请注意,新回拨不包括电话号码。至少对于广播接收器,可以通过以下方式检索电话号码

intent.extras.getString("incoming_number")
于 2022-02-10T09:35:24.803 回答
2

我使用了你所做的并且 android 12 模拟器也可以工作。我将它用于低于 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 类。 添加电话状态监听器

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
        }
    )


}

}

于 2021-11-08T15:40:47.270 回答
1

服务接收器(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);
}

}

于 2021-11-12T14:14:28.323 回答