0

I have an issue in service connection issue in that line var binder:TwentyFourHoursService.LocalBinder = service as TwentyFourHoursService.LocalBinder and could not find solution:

 override fun onServiceConnected(
            className: ComponentName,
            service: IBinder
        ) { // cast the IBinder and get MyService instance
            var binder:TwentyFourHoursService.LocalBinder = service as TwentyFourHoursService.LocalBinder
            myService = binder.getService()
            bound = true
             // serviceCallbacks =this as ServiceCallbacks
            myService!!.setCallbacks(mActivity) // register
        }

Here is my service:

class TwentyFourHoursService : Service() {
    private val mHandler: Handler = Handler() //run on another Thread to avoid crash
    private var mTimer: Timer? = null //timer handling

    // Binder given to clients
    private val binder: IBinder = LocalBinder()


    override fun onBind(intent: Intent): IBinder {
//        throw UnsupportedOperationException("Not yet implemented")
        return binder
    }

    override fun onCreate() {
        if (mTimer != null) // Cancel if already existed
            mTimer!!.cancel() else mTimer = Timer() //recreate new
        mTimer!!.scheduleAtFixedRate(
            TimeDisplay(),
            0,
            notify
        ) //Schedule task

        //Timer().scheduleAtFixedRate(TimeDisplay(),0, notify)
    }
    fun setCallbacks(callbacks: ServiceCallbacks?) {
        serviceCallbacks = callbacks
    }

    override fun onDestroy() {
        super.onDestroy()
        mTimer!!.cancel() //For Cancel Timer
        Toast.makeText(this, "Service is Destroyed", Toast.LENGTH_SHORT).show()
    }

    //class TimeDisplay for handling task
    internal inner class TimeDisplay : TimerTask() {
        override fun run() { // run on another thread
            mHandler.post(Runnable {
                // display toast
               /* if (serviceCallbacks!=null) {
                    serviceCallbacks!!.doSomething()
                }*/
                // Reload current fragment
                // Reload current fragment

//                startActivity(Intent(applicationContext, FitnessSlideMenuScreen::class.java))
//                rFitnessSlideMenuScreen().displaySelectedFragment(HomeFragment())
                Toast.makeText(applicationContext, "Service is running", Toast.LENGTH_SHORT).show()
            })
        }
    }

Error:

***java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.example.beyahfitness.service.TwentyFourHoursService$LocalBinder***
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

2

The issue as I realised was as a result of the way had declared my service in Manifest,

<service
    android:name=".MyService"
    android:enabled="true"
    android:process=":MyService" >

When I try getting the service at;
override fun onServiceConnected(name: ComponentName,service: IBinder)
I kept getting this error;
java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.demo.MyService$LocalBinder

I resolved to refactoring my Service as below;

<service
    android:name=".MyService" >

This apparently solves my problem, hope it helps you too

  • same issue and i just remove process name from manifest file and its works thanks budy :) – hio Oct 21 '20 at 07:06