Last active 3 hours ago

Implementation of service, bounded service in Android

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

No changes

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

No changes

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

1 file changed, 1 insertion, 4 deletions

BoundedHelloService.kt

@@ -7,10 +7,7 @@ class BoundedHelloService : Service() {
7 7
8 8 private val mGenerator = Random()
9 9
10 - val randomNumber: Int get() = mGenerator.nextInt(100)
11 -
12 - // Random number generator.
13 -
10 + val randomNumber: Int get() = mGenerator.nextInt(100)
14 11
15 12 /** Method for clients. */
16 13 val randomNumber: Int

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

1 file changed, 6 insertions, 4 deletions

MyActivity.kt

@@ -1,9 +1,11 @@
1 - val intent = Intent(this, HelloService::class.java)
2 - startService(intent)
3 -
1 + // without bound
2 + override fun onStart() {
3 + super.onStart()
4 + val intent = Intent(this, HelloService::class.java)
5 + startService(intent)
6 + }
4 7
5 8 // with bound
6 -
7 9 private lateinit var mService: LocalService
8 10 private var mBound: Boolean = false
9 11

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

2 files changed, 37 insertions, 2 deletions

MyActivity.kt(file created)

@@ -0,0 +1,37 @@
1 + val intent = Intent(this, HelloService::class.java)
2 + startService(intent)
3 +
4 +
5 + // with bound
6 +
7 + private lateinit var mService: LocalService
8 + private var mBound: Boolean = false
9 +
10 + /** Defines callbacks for service binding, passed to bindService(). */
11 + private val connection = object : ServiceConnection {
12 +
13 + override fun onServiceConnected(className: ComponentName, service: IBinder) {
14 + // We've bound to LocalService, cast the IBinder and get LocalService instance.
15 + val binder = service as LocalService.LocalBinder
16 + mService = binder.getService()
17 + mBound = true
18 + }
19 +
20 + override fun onServiceDisconnected(arg0: ComponentName) {
21 + mBound = false
22 + }
23 + }
24 +
25 + override fun onStart() {
26 + super.onStart()
27 + // Bind to LocalService.
28 + Intent(this, LocalService::class.java).also { intent ->
29 + bindService(intent, connection, Context.BIND_AUTO_CREATE)
30 + }
31 + }
32 +
33 + override fun onStop() {
34 + super.onStop()
35 + unbindService(connection)
36 + mBound = false
37 + }

gistfile1.txt (file deleted)

@@ -1,2 +0,0 @@
1 - val intent = Intent(this, HelloService::class.java)
2 - startService(intent)

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

1 file changed, 27 insertions

BoundedHelloService.kt(file created)

@@ -0,0 +1,27 @@
1 + class BoundedHelloService : Service() {
2 + private val binder = LocalBinder()
3 +
4 + inner class LocalBinder : Binder() {
5 + fun getService(): HelloService = this@HelloService
6 + }
7 +
8 + private val mGenerator = Random()
9 +
10 + val randomNumber: Int get() = mGenerator.nextInt(100)
11 +
12 + // Random number generator.
13 +
14 +
15 + /** Method for clients. */
16 + val randomNumber: Int
17 + get() = mGenerator.nextInt(100)
18 +
19 + override fun onBind(intent: Intent): IBinder {
20 + return binder
21 + }
22 +
23 + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
24 + return START_STICKY
25 + }
26 +
27 + }

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

No changes

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

1 file changed, 6 insertions, 1 deletion

HelloService.kt

@@ -3,8 +3,13 @@ class HelloService : Service() {
3 3 return null
4 4 }
5 5
6 + /*
7 + START_NOT_STICKY
8 + START_STICKY
9 + START_REDELIVER_INTENT
10 + */
6 11 override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
7 - return super.onStartCommand(intent, flags, startId)
12 + return START_STICKY
8 13 }
9 14
10 15 }

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

1 file changed, 1 insertion, 1 deletion

AndroidManifest.xml

@@ -1 +1 @@
1 - <service android:name=".HelloService" />
1 + <service android:name=".HelloService" />

radimkliment's Avatar radimkliment revised this gist 3 hours ago. Go to revision

2 files changed, 2 insertions, 4 deletions

AndroidManifest.xml

@@ -1,5 +1 @@
1 1 <service android:name=".HelloService" />
2 -
3 - Intent(this, HelloService::class.java).also { intent ->
4 - startService(intent)
5 - }

gistfile1.txt(file created)

@@ -0,0 +1,2 @@
1 + val intent = Intent(this, HelloService::class.java)
2 + startService(intent)
Newer Older