Android - Using method from a Service in an Activity?

后端 未结 3 1477
庸人自扰
庸人自扰 2020-12-07 15:06

I have the folowing method in a Service in my appplication:

public void switchSpeaker(boolean speakerFlag){

        if(speakerFlag){
        audio_service.s         


        
3条回答
  •  情歌与酒
    2020-12-07 15:34

    It's public, right :)

    You can call bindService(Intent) method. Tale a look at ApiDemos, the class LocalServiceBinding.

    In the callback method onServiceConnected, you can see:

        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service.  Because we have bound to a explicit
            // service that we know is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            mBoundService = ((LocalService.LocalBinder)service).getService();
    
            // Tell the user about this for our demo.
            Toast.makeText(LocalServiceBinding.this, R.string.local_service_connected,
                    Toast.LENGTH_SHORT).show();
        }
    

    Now, use the service object (mBoundService) to call the method.

    That's all :)

提交回复
热议问题