Skip to content

Commit

Permalink
App/Service: Bound service to MainActivity
Browse files Browse the repository at this point in the history
This patch bounds service to MainActivity.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 authored and jaeyun-jung committed Apr 25, 2024
1 parent 3830e74 commit cdb2cdf
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -25,7 +42,30 @@ class MainActivity : ComponentActivity() {
) { }
}
}
setContentView(R.layout.activity_main)

startForegroundService(Intent(this, MainService::class.java))

val start = findViewById<Button>(R.id.start)
start.setOnClickListener(View.OnClickListener {
mService?.startServer()
})

val stop = findViewById<Button>(R.id.stop)
stop.setOnClickListener(View.OnClickListener {
mService?.stopServer()
})
}

override fun onStart() {
super.onStart()
Intent(this, MainService::class.java).also { intent->
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
}

override fun onStop() {
super.onStop()
unbindService(connection)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.app.Service
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.os.Binder
import android.os.Handler
import android.os.HandlerThread
import android.os.IBinder
Expand Down Expand Up @@ -35,12 +36,16 @@ class MainService : Service() {
}
}

inner class LocalBinder : Binder() {
fun getService(): MainService = this@MainService
}

private val TAG = "MainService"
private val binder = LocalBinder()
private lateinit var serviceHandler : MainHandler
private lateinit var serviceLooper : Looper
private lateinit var handlerThread: HandlerThread
private var initialized = false

private fun startForeground() {
// Get NotificationManager
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
Expand Down Expand Up @@ -105,7 +110,7 @@ class MainService : Service() {
}

override fun onBind(intent: Intent): IBinder {
TODO("Return the communication channel to the service.")
return binder
}

override fun onDestroy() {
Expand All @@ -129,4 +134,12 @@ class MainService : Service() {
}
}
}

fun startServer() {
TODO("Not yet implemented")
}

fun stopServer() {
TODO("Not yet implemented")
}
}
15 changes: 15 additions & 0 deletions ml_inference_offloading/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>

0 comments on commit cdb2cdf

Please sign in to comment.