Skip to content

Commit

Permalink
Added intent service demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ViratAtAndroid committed Nov 18, 2023
1 parent 84613a0 commit f097553
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.jetpack_compose_all_in_one.application_components.services

import android.app.IntentService
import android.content.Context
import android.content.Intent
import android.util.Log
import com.example.jetpack_compose_all_in_one.application_components.services.IntentServiceDemo.Companion.URL_OF_DOWNLOAD_FILE

class IntentServiceDemo : IntentService("IntentServiceDemo") {

companion object {
const val ACTION_DOWNLOAD_FILE = "com.example.intentdemo.action.DOWNLOAD_FILE"
const val EXTRA_FILE_URL = "com.example.intentdemo.extra.FILE_URL"
const val URL_OF_DOWNLOAD_FILE = "https://example.com/samplefile.txt"
}

override fun onHandleIntent(intent: Intent?) {
when (intent?.action) {
ACTION_DOWNLOAD_FILE -> {
val fileUrl = intent.getStringExtra(EXTRA_FILE_URL)
downloadFile(fileUrl)
}
}
}

private fun downloadFile(fileUrl: String?) {
// Simulate a long-running task such as downloading a file
Log.d("IntentServiceDemo", "Downloading file from $fileUrl")

// Simulate the download process
for (progress in 0..100 step 10) {
// Update progress or perform other tasks
Log.d("IntentServiceDemo", "Progress: $progress%")
try {
Thread.sleep(1000) // Simulate time-consuming task
} catch (e: InterruptedException) {
Thread.currentThread().interrupt()
}
}

// Download completed
Log.d("IntentServiceDemo", "Download complete for file from $fileUrl")

// You can send a broadcast or update UI here if needed
// For simplicity, let's log the completion message.
}
}

fun invokeIntentService(context: Context) {
val intent = Intent(context, IntentServiceDemo::class.java).apply {
action = IntentServiceDemo.ACTION_DOWNLOAD_FILE
putExtra(IntentServiceDemo.EXTRA_FILE_URL, URL_OF_DOWNLOAD_FILE)
}
context.startService(intent)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.jetpack_compose_all_in_one.third_party_lib.async_task

import android.annotation.SuppressLint
import android.os.AsyncTask
import android.os.Handler
import android.os.Looper
import android.os.Message

fun main() {
AsyncTaskDemo().execute()
}

class AsyncTaskDemo : AsyncTask<Int, Int, String>() {

override fun onPreExecute() {
super.onPreExecute()
//handle your UI task
}


override fun doInBackground(vararg p0: Int?): String {
//handle your background task

val handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
// Update UI with the calculation result here
}
}
return ""
}

@SuppressLint("WrongThread")
override fun onProgressUpdate(vararg values: Int?) {
super.onProgressUpdate(*values)
publishProgress(values.size)
}

override fun onPostExecute(result: String?) {
super.onPostExecute(result)
//handle the UI task after finishing the background task...
}
}

0 comments on commit f097553

Please sign in to comment.