-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84613a0
commit f097553
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...m/example/jetpack_compose_all_in_one/application_components/services/IntentServiceDemo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
42 changes: 42 additions & 0 deletions
42
...n/java/com/example/jetpack_compose_all_in_one/third_party_lib/async_task/AsyncTaskDemo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... | ||
} | ||
} |