From ced1eae964199629b2ec6b8c4f5499dc5fdf5b27 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 | 50 +++++++++++++++++-- .../ml/inference/offloading/MainService.kt | 17 ++++++- .../src/main/res/layout/activity_main.xml | 15 ++++++ 3 files changed, 75 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..1fed899 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,36 @@ 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 var mService: MainService? = null + + private val connection = object : ServiceConnection { + + override fun onServiceConnected(className: ComponentName, service: IBinder) { + val binder = service as MainService.LocalBinder + mService = binder.getService() + } + + override fun onServiceDisconnected(arg0: ComponentName) { + mService = null + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { @@ -25,7 +42,30 @@ class MainActivity : ComponentActivity() { ) { } } } + setContentView(R.layout.activity_main) startForegroundService(Intent(this, MainService::class.java)) + + val start = findViewById