Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App/Service: Bound service to MainActivity #15

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>