0

I'd like to get cell information updates on an android 12 phone. I can do this for older versions of android using PhoneStateListener class and listen method. But they are deprecated in android 12. I'v tried below code but it doesn't show cellInfo.

val tm: TelephonyManager = this.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
     tm.registerTelephonyCallback(this.mainExecutor, object:TelephonyCallback(), TelephonyCallback.CellInfoListener{
                @SuppressLint("MissingPermission")
                override fun onCellInfoChanged (cellInfo:List<CellInfo>) {
                    testTextView?.text = cellInfo.toString()   }  }    )
        } else { 
             var psListener= MyPhoneStateListener(tm)
             tm.listen(psListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) }

Does any one know what I should do? Thank you in advance.

  • [The documentation for `listen()`](https://developer.android.com/reference/kotlin/android/telephony/TelephonyManager#listen) points you [to `registerTelephonyCallback()`](https://developer.android.com/reference/kotlin/android/telephony/TelephonyManager#registertelephonycallback) for use on API Level 31+ devices. – CommonsWare Oct 09 '22 at 16:26
  • I have read it and I have used it. But it doesn't work. – Sara-Omidvar Oct 10 '22 at 06:37

1 Answers1

2
private Executor executorist= new Executor() {
    @Override
    public void execute(Runnable r) {
        r.run();
    }
};
class CellInfoListener extends TelephonyCallback
        implements TelephonyCallback.CellInfoListener {
    @SuppressLint("MissingPermission")
    @Override
    public void onCellInfoChanged(@NonNull List<CellInfo> list) {
        //FooFighters will do it, but your own function could do it too
        FooFighters(list);
    }
}
CellInfoListener cellists= new CellInfoListener();
tmanager.registerTelephonyCallback(executorist,cellists);

https://cs.android.com/android/platform/superproject/+/android-12.0.0_r33:cts/tests/tests/telephony/current/src/android/telephony/cts/

J-Ape
  • 21
  • 2