Skip to content

Commit ba220bc

Browse files
committed
intentservice code added
1 parent c516a54 commit ba220bc

14 files changed

Lines changed: 105 additions & 0 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CustomIntentService : IntentService("name") {
2+
3+
override fun onHandleIntent(intent: Intent?) {
4+
//retrieve data
5+
val action = intent?.action
6+
val data = intent?.dataString
7+
//do some work based on action
8+
}
9+
10+
//avoid to override Service's callback like onStartCommand
11+
//they are automatically invoked by IntentService
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class IntentServiceCommunication : IntentService("name") {
2+
3+
override fun onHandleIntent(intent: Intent?) {
4+
//retrieve data
5+
val action = intent?.action
6+
val data = intent?.getStringExtra("extra")
7+
//do some work based on action
8+
9+
sendBroadcast()
10+
}
11+
12+
private fun sendBroadcast() {
13+
val broadcastIntent = Intent("FILTER_ACTION")
14+
broadcastIntent.putExtra("message", "result")
15+
sendBroadcast(broadcastIntent)
16+
Log.d("IntentService", "sendBroadcast")
17+
}
18+
}

intentservice/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# intentservice
2+
This is repository of http://androidcode.pl blog. It shows uses IntentService in Android. It is a part of Background processing - IntentService post in the blog.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class CommunicationActivity : AppCompatActivity() {
2+
3+
private val receiver = CustomBroadcastReceiver()
4+
5+
override fun onCreate(savedInstanceState: Bundle?) {
6+
super.onCreate(savedInstanceState)
7+
setContentView(R.layout.activity_communication)
8+
button.setOnClickListener { startIntentService() }
9+
10+
registerReceiver(receiver, IntentFilter("FILTER_ACTION"))
11+
}
12+
13+
override fun onDestroy() {
14+
//if service is running and don't need anymore callback then stop IntentService
15+
unregisterReceiver(receiver)
16+
super.onDestroy()
17+
}
18+
19+
fun startIntentService() {
20+
val intent = Intent(this, CustomIntentService::class.java)
21+
intent.action = "action"
22+
intent.putExtra("extra", "value")
23+
startService(intent)
24+
}
25+
26+
//implement as nested class if only need to interact with this activity
27+
class CustomBroadcastReceiver : BroadcastReceiver() {
28+
29+
override fun onReceive(context: Context?, intent: Intent?) {
30+
val message = intent?.getStringExtra("message")
31+
//do something like update UI
32+
}
33+
}
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class CustomActivity : AppCompatActivity() {
2+
3+
override fun onCreate(savedInstanceState: Bundle?) {
4+
super.onCreate(savedInstanceState)
5+
setContentView(R.layout.activity_custom)
6+
button.setOnClickListener { startIntentService() }
7+
}
8+
9+
//just call startService to run IntentService
10+
private fun startIntentService() {
11+
val intent = Intent(this, CustomIntentService::class.java)
12+
intent.action = "action"
13+
intent.putExtra("extra", "value")
14+
startService(intent)
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- remember to add Service to AndroidManifest in the same way like base JobService
2+
//with required BINB_JOB_SERVICE permission -->
3+
<service
4+
android:name=".CustomJobIntentService"
5+
android:permission="android.permission.BIND_JOB_SERVICE"/>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class CustomJobIntentService : JobIntentService() {
2+
3+
companion object {
4+
const val ID = 100
5+
6+
//method for enqueuing work to this service, just call from client to start
7+
fun enqueueWork(context : Context, work : Intent) {
8+
enqueueWork(context, CustomJobIntentService::class.java, ID, work)
9+
}
10+
}
11+
12+
override fun onHandleWork(intent: Intent) {
13+
//do some job here
14+
//use BroadcastReceiver or something else to post the result
15+
}
16+
}

service/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# service
2+
This is repository of http://androidcode.pl blog. It shows uses Service in Android. It is a part of Background processing - Service post in the blog.

0 commit comments

Comments
 (0)