Reputation: 1473
I'd like to listen if there's a phone call happening while my app is in the foreground.
It was like this before but now listen() is deprecated:
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)
I tried something like this but I couldn't find the correct way to implement it.
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")
}
}
}
}
Upvotes: 14
Views: 22338
Reputation: 723
Since the listen method is deprecated since api 31 android 12 I made a simple way to listen to the telephony callbacks.
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)
}
Note that the new callback does not include a phone number. At least for broadcast receiver the phone number can be retrieved via
intent.extras.getString("incoming_number")
Upvotes: 17
Reputation: 131
ServiceReceiver(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);
}
}
Upvotes: 4
Reputation: 131
I used what you did and android 12 emulator also worked.I used this for versions less than android 12 I hope it works.
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")
}
}
}
}
I used this to listen to the call
phoneListener = MyPhoneStateListener()
var telephony: TelephonyManager? = null
telephony = context
.getSystemService(TELEPHONY_SERVICE) as TelephonyManager?
telephony!!.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE)
finally you should make a ServiceReceiver class like here. Add PhoneStateListener
Android 12 :I found an example here. I hope it works for you. 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
}
)
}
Manifest
<receiver android:name=".data.service.receiver.ServiceReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
ServiceReceiver
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
}
)
}
}
Upvotes: 2