From b9ca1b1b286f4db235a6e6e6aafb6dc52e246c83 Mon Sep 17 00:00:00 2001 From: Yelin Jeong Date: Mon, 22 Apr 2024 20:56:21 +0900 Subject: [PATCH] App/Service: Bound service to MainActivity This patch bounds service to MainActivity. Signed-off-by: Yelin Jeong --- .../ml/inference/offloading/MainActivity.kt | 58 +++++++++++++++++-- .../ml/inference/offloading/MainService.kt | 17 +++++- .../src/main/res/layout/activity_main.xml | 15 +++++ 3 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 ml_inference_offloading/src/main/res/layout/activity_main.xml diff --git a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainActivity.kt b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainActivity.kt index 395ceb0..7878702 100644 --- a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainActivity.kt +++ b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainActivity.kt @@ -1,19 +1,39 @@ package ai.nnstreamer.ml.inference.offloading +import ai.nnstreamer.ml.inference.offloading.ui.theme.NnstreamerandroidTheme +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.View +import android.widget.Button import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier -import androidx.compose.ui.tooling.preview.Preview -import ai.nnstreamer.ml.inference.offloading.ui.theme.NnstreamerandroidTheme -import android.content.Intent class MainActivity : ComponentActivity() { + private val TAG = "MainActivity" + private lateinit var mService: MainService + private var mBound: Boolean = false + + private val connection = object : ServiceConnection { + + override fun onServiceConnected(className: ComponentName, service: IBinder) { + val binder = service as MainService.LocalBinder + mService = binder.getService() + mBound = true + } + + override fun onServiceDisconnected(arg0: ComponentName) { + mBound = false + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { @@ -25,7 +45,35 @@ class MainActivity : ComponentActivity() { ) { } } } + setContentView(R.layout.activity_main) startForegroundService(Intent(this, MainService::class.java)) + + val start = findViewById