Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
niley7464 committed Apr 24, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 0b47336 commit dc06d5f
Showing 4 changed files with 83 additions and 8 deletions.
1 change: 1 addition & 0 deletions ml_inference_offloading/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:icon="@mipmap/ic_launcher"
Original file line number Diff line number Diff line change
@@ -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,33 @@ class MainActivity : ComponentActivity() {
) { }
}
}
setContentView(R.layout.activity_main)

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

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

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

startForegroundService(Intent(this, MainService::class.java))
override fun onStop() {
super.onStop()
unbindService(connection)
mBound = false
}
}
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -101,7 +106,7 @@ class MainService : Service() {
}

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

override fun onDestroy() {
@@ -125,4 +130,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 dc06d5f

Please sign in to comment.