Created
December 5, 2022 10:35
-
-
Save alokomkar/c124787b0fed992b2fd76afb29e62454 to your computer and use it in GitHub Desktop.
HomeFragment Bound Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.alokomkar.activity | |
| import android.content.ComponentName | |
| import android.content.Context | |
| import android.content.Intent | |
| import android.content.ServiceConnection | |
| import android.os.Bundle | |
| import android.os.IBinder | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import android.widget.Toast | |
| import androidx.appcompat.widget.AppCompatButton | |
| import androidx.core.content.ContextCompat | |
| import androidx.fragment.app.Fragment | |
| import androidx.fragment.app.viewModels | |
| import com.alokomkar.javacollections.R | |
| import com.alokomkar.service.AudioForegroundLoopService | |
| import com.alokomkar.service.AudioLoopBoundService | |
| import com.alokomkar.service.AudioLoopService | |
| import com.alokomkar.service.ServiceAction | |
| import com.alokomkar.viewmodel.HomeViewModel | |
| class HomeFragment: Fragment() { | |
| ... | |
| private val audioLoopBoundServiceIntent: Intent by lazy { | |
| Intent( | |
| context, | |
| AudioLoopBoundService::class.java | |
| ) | |
| } | |
| private var audioLoopBoundService: AudioLoopBoundService? = null | |
| private val boundServiceConnection by lazy { | |
| object : ServiceConnection { | |
| override fun onServiceConnected(className: ComponentName, service: IBinder) { | |
| val binder: AudioLoopBoundService.AudioLoopServiceBinder = service as AudioLoopBoundService.AudioLoopServiceBinder | |
| audioLoopBoundService = binder.getService() | |
| viewModel.isAudioServiceBound = true | |
| } | |
| override fun onServiceDisconnected(arg0: ComponentName) { | |
| audioLoopBoundService?.runAction(ServiceAction.STOP_ACTION) | |
| audioLoopBoundService = null | |
| viewModel.isAudioServiceBound = false | |
| } | |
| } | |
| } | |
| override fun onCreateView( | |
| inflater: LayoutInflater, | |
| container: ViewGroup?, | |
| savedInstanceState: Bundle? | |
| ): View? { | |
| return inflater.inflate(R.layout.fragment_home, container, false) | |
| } | |
| override fun onStart() { | |
| super.onStart() | |
| // bind to service if it isn't bound | |
| if (!viewModel.isAudioServiceBound) bindToAudioService() | |
| } | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| view.apply { | |
| ... | |
| findViewById<AppCompatButton>(R.id.startButtonBoundService).setOnClickListener { | |
| sendCommandToBoundService(ServiceAction.START_ACTION) | |
| } | |
| findViewById<AppCompatButton>(R.id.stopButtonBoundService).setOnClickListener { | |
| sendCommandToBoundService(ServiceAction.STOP_ACTION) | |
| } | |
| } | |
| } | |
| private fun sendCommandToBoundService(action: ServiceAction) { | |
| if(viewModel.isAudioServiceBound) { | |
| audioLoopBoundService?.runAction(action) | |
| } | |
| else Toast.makeText( | |
| context, | |
| "Service not bound yet", | |
| Toast.LENGTH_SHORT | |
| ).show() | |
| } | |
| override fun onDestroy() { | |
| super.onDestroy() | |
| unbindAudioService() | |
| } | |
| private fun unbindAudioService() { | |
| if(viewModel.isAudioServiceBound) { | |
| audioLoopBoundService?.runAction(ServiceAction.STOP_ACTION) | |
| context?.unbindService(boundServiceConnection) | |
| } | |
| } | |
| private fun bindToAudioService() { | |
| context?.bindService( | |
| audioLoopBoundServiceIntent, | |
| boundServiceConnection, | |
| Context.BIND_AUTO_CREATE | |
| ) | |
| } | |
| ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment