androidkotlinlistenertelephonymanagercellinfo

How to use TelephonyCallback.CellInfoListener?


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.


Solution

  • 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/