I have a Service that acts like a TimerTask and even if I stop the service using :
val intent = Intent(applicationContext, TimeService::class.java)
stopService(intent)
It doesn't stop, I have a Log in onDestroy and this Log is fired... but I think the problem is that I'm using a TimerTask inside the Service this is my Service
class TimeService : Service() {
private val mHandler = Handler()
var calendar: Calendar? = null
var simpleDateFormat: SimpleDateFormat? = null
var strDate: String? = null
var date_current: Date? = null
var date_diff: Date? = null
private var mTimer: Timer? = null
private val NOTIFY_INTERVAL: Long = 1000
var intent: Intent? = null
companion object {
val str_receiver = "myreceiver"
}
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onCreate() {
super.onCreate()
calendar = Calendar.getInstance()
simpleDateFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
mTimer = Timer()
mTimer!!.scheduleAtFixedRate(TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL)
intent = Intent(str_receiver)
}
internal inner class TimeDisplayTimerTask : TimerTask() {
override fun run() {
mHandler.post {
calendar = Calendar.getInstance()
simpleDateFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
strDate = simpleDateFormat!!.format(calendar!!.time)
Log.e("strDate", strDate)
twoDatesBetweenTime()
}
}
}
fun twoDatesBetweenTime(): String {
try {
date_current = simpleDateFormat!!.parse(strDate)
} catch (e: Exception) {
}
try {
date_diff = simpleDateFormat!!.parse(SharedPreferenceHelper.defaultPrefs(this).getString("data", ""))
} catch (e: Exception) {
}
try {
val diff = date_current!!.time - date_diff!!.time
val timeInSeconds = Integer.valueOf(SharedPreferenceHelper.defaultPrefs(this).getString("seconds", "")!!)
val timeTimer = TimeUnit.SECONDS.toMillis(timeInSeconds.toLong())
val diffWithTime = timeTimer - diff
val diffSeconds2 = diffWithTime / 1000 % 60
val diffMinutes2 = diffWithTime / (60 * 1000) % 60
val diffHours2 = diffWithTime / (60 * 60 * 1000) % 24
if (diffWithTime >= 0) {
val counterTime = "$diffHours2 : $diffMinutes2 : $diffSeconds2"
Log.e("TIME", counterTime)
fn_update(counterTime)
} else {
SharedPreferenceHelper.defaultPrefs(this).edit().putBoolean("finish", true).apply()
mTimer!!.cancel()
}
} catch (e: Exception) {
mTimer!!.cancel()
mTimer!!.purge()
}
return ""
}
override fun onDestroy() {
super.onDestroy()
Log.e("Service finish", "Finish")
}
private fun fn_update(str_time: String) {
intent!!.putExtra("time", str_time)
sendBroadcast(intent)
}
}
And the problem is that this log :
Log.e("strDate", strDate)
And this log :
Log.e("TIME", counterTime)
Never stops, what I'm missing?
EDIT
My approach is this from the moment but I don't know if it's the best way :
override fun onDestroy() {
super.onDestroy()
Log.e("Service finish", "Finish")
if(mTimer!=null){
mTimer!!.cancel()
mTimer!!.purge()
}
}
OnDestroy is a callback method invoked by system to allow your service a "clean exit":
Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up any resources it holds (threads, registered receivers, etc) at this point. Upon return, there will be no more calls in to this Service object and it is effectively dead. Do not call this method directly.
System does not instantly terminate your app process following this callback. It's up to You to kill the TimerTask at this point. If You leave it running it's considered a leak. Most likely it will keep running until system decides it's time to kill Your apps process which might take a while if it's kept in the foreground.
Related
i am working on an online shopping application using retrofit, coroutine, livedata, mvvm,...
i want to show progress bar before fetching data from server for afew seconds
if i have one api request i can show that but in this app i have multiple request
what should i do in this situation how i should show progress bar??
Api Service
#GET("homeslider.php")
suspend fun getSliderImages(): Response<List<Model.Slider>>
#GET("amazingoffer.php")
suspend fun getAmazingProduct(): Response<List<Model.AmazingProduct>>
#GET("handsImages.php")
suspend fun getHandsFreeData(
#Query(
"handsfree_id"
) handsfree_id: Int
): Response<List<Model.HandsFreeImages>>
#GET("handsfreemoreinfo.php")
suspend fun gethandsfreemoreinfo(): Response<List<Model.HandsFreeMore>>
#GET("wristmetadata.php")
suspend fun getWristWatchMetaData(
#Query(
"wrist_id"
) wrist_id: Int
): Response<List<Model.WristWatch>>
repository
fun getSliderImages(): LiveData<List<Model.Slider>> {
val data = MutableLiveData<List<Model.Slider>>()
val job = Job()
applicationScope.launch(IO + job) {
val response = api.getSliderImages()
withContext(Main + SupervisorJob(job)) {
data.value = response.body()
}
job.complete()
job.cancel()
}
return data
}
fun getAmazingOffer(): LiveData<List<Model.AmazingProduct>> {
val data = MutableLiveData<List<Model.AmazingProduct>>()
val job = Job()
applicationScope.launch(IO + job) {
val response = api.getAmazingProduct()
withContext(Main + SupervisorJob(job)) {
data.value = response.body()
}
job.complete()
job.cancel()
}
return data
}
fun getHandsFreeData(handsree_id: Int): LiveData<List<Model.HandsFreeImages>> {
val dfData = MutableLiveData<List<Model.HandsFreeImages>>()
val job = Job()
applicationScope.launch(IO + job) {
val response = api.getHandsFreeData(handsree_id)
withContext(Main + SupervisorJob(job)) {
dfData.value = response.body()
}
job.complete()
job.cancel()
}
return dfData
}
fun getHandsFreeMore(): LiveData<List<Model.HandsFreeMore>> {
val data = MutableLiveData<List<Model.HandsFreeMore>>()
val job = Job()
applicationScope.launch(IO + job) {
val response = api.gethandsfreemoreinfo()
withContext(Main + SupervisorJob(job)) {
data.value = response.body()
}
job.complete()
job.cancel()
}
return data
}
VIEWMODEL
fun getSliderImages() = repository.getSliderImages()
fun getAmazingOffer() = repository.getAmazingOffer()
fun recieveAdvertise() = repository.recieveAdvertise()
fun dailyShoes(context: Context) = repository.getDailyShoes(context)
i will appreciate your help
I couldn't help but notice that your repository contains lots of repetitive code. first point to learn here is that all that logic in Repository, it usually goes in the ViewModel. second thing is that you are using applicationScope to launch your coroutines, which usually is done using viewModelScope(takes care of cancellation) object which is available in every viewModel.
So first we have to take care of that repetitive code and move it to ViewModel. So your viewModel would now look like
class YourViewModel: ViewModel() {
// Your other init code, repo creation etc
// Live data objects for progressBar and error, we will observe these in Fragment/Activity
val showProgress: MutableLiveData<Boolean> = MutableLiveData()
val errorMessage: MutableLiveData<String> = MutableLiveData()
/**
* A Generic api caller, which updates the given live data object with the api result
* and internally takes care of progress bar visibility. */
private fun <T> callApiAndPost(liveData: MutableLiveData<T>,
apiCall: () -> Response<T> ) = viewModelScope.launch {
try{
showProgress.postValue(true) // Show prgress bar when api call is active
if(result.code() == 200) { liveData.postValue(result.body()) }
else{ errorMessage.postValue("Network call failed, try again") }
showProgress.postValue(false)
}
catch (e: Exception){
errorMessage.postValue("Network call failed, try again")
showProgress.postValue(false)
}
}
/******** Now all your API call methods should be called as *************/
// First declare the live data object which will contain the api result
val sliderData: MutableLiveData<List<Model.Slider>> = MutableLiveData()
// Now call the API as
fun getSliderImages() = callApiAndPost(sliderData) {
repository.getSliderImages()
}
}
After that remove all the logic from Repository and make it simply call the network methods as
suspend fun getSliderImages() = api.getSliderImages() // simply delegate to network layer
And finally to display the progress bar, simply observe the showProgress LiveData object in your Activity/Fragment as
viewModel.showProgress.observer(this, Observer{
progressBar.visibility = if(it) View.VISIBLE else View.GONE
}
First create a enum class status:
enum class Status {
SUCCESS,
ERROR,
LOADING
}
Then create resource class like this:
data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
companion object {
fun <T> success(data: T?): Resource<T> {
return Resource(Status.SUCCESS, data, null)
}
fun <T> error(msg: String, data: T?): Resource<T> {
return Resource(Status.ERROR, data, msg)
}
fun <T> loading(data: T?): Resource<T> {
return Resource(Status.LOADING, data, null)
}
}
}
Now add your request to a list of response:
var list = java.util.ArrayList<Response<*>>()
suspend fun getApis() = list.addAll(
listOf(
api.advertise(),
api.getAmazingProduct(),
api.dailyShoes(),
api.getSliderImages(),
.
.
.
)
)
In your viewmodel class:
private val _apis = MutableLiveData<Resource<*>>()
val apis: LiveData<Resource<*>>
get() = _apis
init {
getAllApi()
}
fun getAllApi() {
val job = Job()
viewModelScope.launch(IO + job) {
_apis.postValue(
Resource.loading(null)
)
delay(2000)
repository.getApis().let {
withContext(Main + SupervisorJob(job)) {
it.let {
if (it) {
_apis.postValue(Resource.success(it))
} else {
_apis.postValue(Resource.error("Unknown error eccured", null))
}
}
}
}
job.complete()
job.cancel()
}
}
Now you can use status to show progress like this . use this part in your target fragment:
private fun setProgress() {
viewModel.apis.observe(viewLifecycleOwner) {
when (it.status) {
Status.SUCCESS -> {
binding.apply {
progress.visibility = View.INVISIBLE
line1.visibility = View.VISIBLE
parentscroll.visibility = View.VISIBLE
}
}
Status.ERROR -> {
binding.apply {
progress.visibility = View.INVISIBLE
line1.visibility = View.INVISIBLE
parentscroll.visibility = View.INVISIBLE
}
}
Status.LOADING -> {
binding.apply {
progress.visibility = View.VISIBLE
line1.visibility = View.INVISIBLE
parentscroll.visibility = View.INVISIBLE
}
}
}
}
}
I hope you find it useful.
Context: I found few tutorials explaining how consume mutilple endpoints from Kotlin at same time but they are based on Android and in my case it is a backend application. I have some experience using CompleteableFuture but I assume I should use Coroutine since it is a Kotlin and there is no Spring dependency.
Following some suggestions, I reached
#Singleton
class PersonEndpoint()
{
#Inject
lateinit var employeClient: EmployeClient
override suspend fun getPersonDetails(request: PersonRequest): PersonResponse {
var combinedResult: String
GlobalScope.launch {
val resultA: String
val resultB: String
val employeesA = async{ employeClient.getEmployeesA()}
val employeesB = async{ employeClient.getEmployeesB()}
try{
combinedResult = employeesA.await() + employeesB.await()
print(combinedResult)
} catch (ex: Exception) {
ex.printStackTrace()
}
// ISSUE 1
if I try add return over here it is not allowed.
I understand it is working how it is designed to work: GlobalScope is running in different thread
}
// ISSUE 2
if I try return combinedResult over here combinedResult isn't initialized.
I understand it is working how it is designed to work: GlobalScope is running in different thread and I can
debug and see that return over here executes earlier than employeesA.await = employeesB.await
}
So, how can I execute combinedResult = employeesA.await() + employeesB.await() before returning to the client?
*** Edited after Denis/ answer
#Singleton
class CustomerEndpoint(){
fun serve(): Collection<Int> {
return runBlocking {
async {
getItemDouble(1)
}
async {
getItemTriple(1)
}
}.map { it.await() }
}
suspend fun getItemDouble(i: Int): Int {
delay(1000)
return i * 2
}
suspend fun getItemTriple(i: Int): Int {
delay(1000)
return i * 3
}
override suspend fun getPersonDetails(request: PersonRequest): PersonResponse {
val result = serve()
println("Got result $result")
...
}
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlin.system.measureTimeMillis
fun main() {
val durationMs = measureTimeMillis {
val result = serve()
println("Got result $result")
}
println("The processing is done in $durationMs ms")
}
fun serve(): Collection<Int> {
return runBlocking {
(1..2).map {
async {
getItem(it)
}
}.map { it.await() }
}
}
suspend fun getItem(i: Int): Int {
delay(1000) // Emulate item retrieval work
return i * 2
}
Note that here we have two nested calls - getItem(1) and getItem(2). We can see that they are executed in parallel as overall running time is ~1 second.
Edited in August 05th 2021
private suspend fun myMethod(): List<Any> {
return runBlocking {
listOf(
async { method1() },
async { method2() }
).map { it.await() }
}
}
method1 and method2 are methods calling different endpoints.
I am trying to access the googleFit API.
It seems pretty straightforward. Get the google sign-in permissions and required authorizations then query for Step count.
My code doesn't seem to work.
When I debug this the fitnessOption declaration part throws "source code doesn't match byte code" error. I cleaned my project, rebuild it it didn't work
Android gurus, Where am I going wrong??
fun getAuthorizationAndReadData() {
try {
MainActivity().fitSignIn(FitActionRequestCode.READ_DATA)
} catch () {
Log.i("e", "error!!!!")
}
}
MainActivity
enum class FitActionRequestCode {
READ_DATA
}
private val fitnessOptions: GoogleSignInOptionsExtension = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ).build()
fun fitSignIn(requestCode: FitActionRequestCode) {
if (oAuthPermissionsApproved()) {
readHistoryData()
} else {
requestCode.let {
GoogleSignIn.requestPermissions(
this,
requestCode.ordinal,
getGoogleAccount(), fitnessOptions)
}
}
}
private fun getGoogleAccount() = GoogleSignIn.getAccountForExtension(this, fitnessOptions)
private fun oAuthPermissionsApproved() = GoogleSignIn.hasPermissions(getGoogleAccount(), fitnessOptions)
private fun performActionForRequestCode(requestCode: FitActionRequestCode) = when (requestCode) {
FitActionRequestCode.READ_DATA -> readHistoryData()
}
private fun readHistoryData(): Task<DataReadResponse> {
// Begin by creating the query.
val readRequest = queryFitnessData()
// Invoke the History API to fetch the data with the query
return Fitness.getHistoryClient(this, getGoogleAccount())
.readData(readRequest)
.addOnSuccessListener { dataReadResponse ->
printData(dataReadResponse)
Log.i(ContentValues.TAG, "Data read was successful!") }
.addOnFailureListener { e ->
Log.e(ContentValues.TAG, "There was a problem reading the data.", e)
}
}
private fun queryFitnessData(): DataReadRequest {
// [START build_read_data_request]
// Setting a start and end date using a range of 1 week before this moment.
val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
val now = Date()
calendar.time = now
val endTime = calendar.timeInMillis
calendar.add(Calendar.WEEK_OF_YEAR, -1)
val startTime = calendar.timeInMillis
return DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build()
}
Try following steps once.
Click Build -> Clean then Disable Instant Run, in Settings -> Build, Execution, Deployment
I am using Exoplayer to play some videos. I have some .mpd live url's which comes from a backend. Normally if the broadcast is live, the seek bar is set to right end side and on the left end side live video cache time is stated with minus value like (-00.59.51). I want to achive that in my project. But by default, current position of the video stated on left side and video duration on right side.
There is two problem:
1.We have to detect if the video is live or not.
2.We need to set time values for live video.
private fun initializePlayerXml(currentXmlLink: String) {
if (player == null) {
val trackSelector = DefaultTrackSelector(this)
trackSelector.setParameters(
trackSelector.buildUponParameters().setMaxVideoSizeSd()
)
try {
player = SimpleExoPlayer.Builder(this)
.build()
} catch (e: Exception) {
}
}
if (player!!.bufferedPosition == 0L) {
playButton.setImageResource(R.drawable.ic_pause)
}
playButton.setOnClickListener {
if (player!!.isPlaying) {
player!!.pause()
playButton.setImageResource(R.drawable.ic_play)
} else {
player!!.play()
playButton.setImageResource(R.drawable.ic_pause)
}
}
val doubleClickForwardFun = DoubleClick(object : DoubleClickListener {
override fun onSingleClickEvent(view: View?) {
}
override fun onDoubleClickEvent(view: View?) {
val currentPosition = player!!.currentPosition
player!!.seekTo(currentPosition + 10000)
}
})
val doubleClickBackwardFun = DoubleClick(object : DoubleClickListener {
override fun onSingleClickEvent(view: View?) {
}
override fun onDoubleClickEvent(view: View?) {
val currentPosition = player!!.currentPosition
player!!.seekTo(currentPosition - 10000)
}
})
doubleClickBackward.setOnClickListener(doubleClickBackwardFun)
doubleClickForward.setOnClickListener(doubleClickForwardFun)
forwardButton.setOnClickListener {
val currentPosition = player!!.currentPosition
player!!.seekTo(currentPosition + 10000)
}
backwardButton.setOnClickListener {
val currentPosition = player!!.currentPosition
player!!.seekTo(currentPosition - 10000)
}
val mediaItem =
MediaItem.Builder()
.setUri(currentXmlLink)
.setMimeType(MimeTypes.APPLICATION_MPD)
.build()
player!!.addMediaItem(mediaItem)
playerView!!.player = player
player!!.playWhenReady = playWhenReady
player!!.seekTo(currentWindow, playbackPosition)
playbackStateListener.let { player!!.addListener(it) }
player!!.prepare()
}
I have already a question on Exoplayer/Issues, you can check here. Also my repo is here.
I am doing Video Triming and using k4l-video-trimmer library. I am getting an issue. I have downloaded the latest code and integrate it on Android Studio. When i select a video, k4l-video-trimmer successfully prepared the video and correctly shows video info and snapshots. I have set the max duration to 10 sec but when move the progressbar to crop the video at specific duration, the cropping duration which is showing on screen like (01:21 sec - 01:31 sec) for 10 sec will change to (01:21 sec - 01:36 sec) becomes 15 sec duration that is an issue and when I crop the video, it will crop it for 23 sec. I don't know how to resolve this issue. Please help me to resolve this issue
You have to implement MediaRecorder.OnInfoListener to manually stop the recording at 10 seconds. Once its stopped, the MediaRecorder goes back to the initial state and the setup has to be done again to start back recording.
public class VideoCapture extends Activity implements MediaRecorder.OnInfoListener {
public void startVideoRecording() {
// Normal MediaRecorder Setup
recorder.setMaxDuration(10000); // 10 seconds
recorder.setOnInfoListener(this); // very important
}
public void onInfo(MediaRecorder mrc, int mri, int extra) {
if (mri == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
Log.v("VIDEOCAPTURE","10 seconds");
mrc.stop();
}
}
}
Now, For the Progress bar you can use a Timer.
//fires once a second, decrease this to fire more frequently
private static final int TIMER_FREQ = 1000;
final ProgressBar progressBar = new ProgressBar(this); //where this is a Context
progressBar.setMax(10000);
Timer progressBarAdvancer = new Timer();
progressBarAdvancer.scheduleAtFixedRate(new TimerTask() {
public void run() {
progressBar.setProgress(progressBar.getProgress() + TIMER_FREQ);
}
},
0, //Delay before first execution
TIMER_FREQ);
By doing this, the progressBar operates on a separate thread from the recording, but will finish within the required 10 seconds. At this pount you can stop the recording and do the rest of the things.
Also, you can use the Video Trimmer based on "k4l-video-trimmer" library which handle various issues on the k4l-video-trimmer.
You can use mobile-ffmpeg Supports API Level 16+
fun scaleVideo(path: String, destinationFilePath: String) {
_loaderVisisble.value = true
viewModelScope.launch {
val cmd = arrayOf(
"-i",
path,
"-vf",
"scale=576:1024:force_original_aspect_ratio=decrease",
destinationFilePath
)
Log.v("str_Cmd", cmd.toString() + "")
val status = executeCommand(cmd)
when (status) {
FFmpeg.RETURN_CODE_SUCCESS -> {
_loaderVisisble.value = false
val mergedFile = File(destinationFilePath)
Log.v(
"target_file_size",
(mergedFile.length() / 1024).toString().toInt().toString() + ""
)
onVideoScaleListener.postValue(destinationFilePath)
}
FFmpeg.RETURN_CODE_CANCEL -> {
_loaderVisisble.value = false
}
else -> {
_loaderVisisble.value = false
}
}
}
}
private suspend fun executeCommand(cmd: Array<String>): Int {
var status = -1
withContext(Dispatchers.Default) {
val rc = FFmpeg.execute(cmd)
when (rc) {
FFmpeg.RETURN_CODE_SUCCESS -> {
Log.i(
Config.TAG,
"Command execution completed successfully."
)
}
FFmpeg.RETURN_CODE_CANCEL -> {
Log.i(
Config.TAG,
"Command execution cancelled by user."
)
}
else -> {
Log.i(
Config.TAG,
String.format(
"Command execution failed with rc=%d and the output below.",
rc
)
)
}
}
status = rc
}
return status
}