My Android service is not working when the application gets destroyed in Xiaomi devices, but it works perfectly with Motorola devices. Currently I have tested my app with Motorola G 2nd generation, Redmi Note 3 and Mi Pad.
I want to play music in the background when my app is not in recents.
Intent i = new Intent(CollapsingToolbarActivity.this, SongService.class);
startService(i);
well, i got your point, may be u are missing with permission in security app of xiaomi devices. you must allow your app to autostart. that will prevent your service from destroying.
#K. Gandhi He is not saying the app is destroying when using xiaomi device, he says that onDestroy(); seems not working on xiaomi device.
#meeta mastani You have to be clear on your question, but as I understand your question, You are using ForegroundService that you want to run your app even when onDestoy(); is being called or the app is closed.
Answer:
Maybe you are using heavy work on your thread, you have to consider using asyncTask, BackgroundThread, BackgroundService and ForegroundService.
asyncTask - for network operations (internet).
BackgroundThread - for heavy work, but when phone sleeps or displays
turned off, your app will not run or music will not play if you are
making music app.
BackgroundService - your app is in the background but still active
(minimize)
ForegroundService - you app is close or destroy but still working or
playing music.
Related
I'm trying to create service that plays music in background. Everything works fine, except that the service is stopping after some time when the phone is locked. To be exact - my service logic looks like that:
after pressing start it plays music (playNext function is called), when the music ends the playNext function is called again. For some time it works perfectly, but when the phone is locked after a few songs, the song is ending and the next one is not starting until I press any physical button to wake up the phone.
The service is being run in foreground with a Notification, onStartCommand returns START_STICKY.
I'm using Android 10, my phone is Xiaomi Mi A2 lite.
As far as I remember issues like that weren't happening to me before - is it some new 'feature' in Android10 to workaround? And how to do that if yes? Or maybe it's my phone model problem and nothing can be done here?
It seems I have found a reason.
The problem is with my phone. After upgrade to android 10 there are some bugs (for example different music players are spontaneously pausing music).
I am creating an Android app (Java code) that has an audio call feature.
I managed to make it work using the webRTC framework.
I wanted to make my audio call behave like WhatsApp and Messenger, where those apps keep the audio call running even if the user stops the app from apps history, or starts using another app.
So, I decided to migrate the webRTC code from an Activity to a Foreground Service.
My Audio Call Activity handles the UI, displaying friend photo, name, call timer ...
So I had to make communication between my Activity and my Foreground Service through binding and sending android.os.Message object.
Everything works fine when using SAMSUNG devices, if I kill the app, I could return to the Call activity by clicking the foreground service's notification, and the audio call keeps working until I hang up.
My problem appeared when i used a Xiaomi device (redmi note7), if i kill the app, the audio call stops, cause my Foreground Service is restarted.
Even after enabling "Autostart" from settings.
I searched in here for a solution but all the answers i found didn't satisfy my need, cause i can't just accept my Service being restarted.
If i want to resume the audio call, i have to re-call the friend again !
So, the question is :
How to prevent Xiaomi, Huawei, Oppo ... devices from restarting Foreground Service ?
Which is the case with WhatsAPP, Messenger, Instagram and others.
It's a tricky question since it really depends on the vendor. There is a site which document and rank the vendor according to "how bad" they're handling services and processes. From the site:
...With Android 6 (Marshmallow), Google has introduced Doze mode to the base Android, in an attempt to unify battery saving across the various Android phones. Unfortunately, vendors (e.g. Xiaomi, Huawei, OnePlus or even Samsung..) did not seem to catch that ball and they all have their own battery savers, usually very poorly written, saving battery only superficially with side effects.
The solutions differs from vendor to vendor and from ROM version to another.
For example for Huawei you can overcome the issue in Huawei P20, Huawei P20 Lite, Huawei Mate 10 by Phone settings > Battery > App launch and then set your app to “Manage manually” and make sure everything is turned on. Also for reliable background processes you may need to uninstall PowerGenie (which is a power mgmt. application by Huawei). On the other hand for EMUI 9+ devices you'll have to uninstall PowerGenie via adb
I am developing a Fitness Application as part of my Bachelor Thesis, and want to keep track of step counts even when the application is completely closed. For this I am currently starting a service that utilises the built in Sensors "Step Counter" and "Step Detector". After some testing I found out that sometimes my Service gets killed and no longer keeps track of the steps taken. I left the phone on my desk overnight and walked around in the morning then I opened the application and the steps I took in the morning were not tracked, whereas when I close the application and immediatly start walking the tracking of steps still works.
Is there a way to make sure that my Service does not get killed?
Would the use of a Foreground Service solve my issue and are there any alternatives to using a foreground service?
Foreground Service is the only way if you want to assure that the service will not be killed.
The reason for this is that the foreground service always shows a notification to the user and can be killed by the user if he wants to, this is especially important if you want to know for sure what runs on your device.
All previous methods of making permanent running services are deprecated starting from android 9, when a new privacy policy was introduced.
Basically you need to keep service running in the background,
Here is the workaround to achieve this
https://stackoverflow.com/a/58162451/7579041
above link is useful for Stock ROM & Custom ROM Devices like OnePlus, OPPO, VIVO, etc
I hope this will help you out
how the background services in whatsapp, facebook, instagram are running even if the app is closed in android versions >=N. also please suggest how to approach this problem
I am trying to get the current GPS location and update in server as a background service
(Note : I am using Gionee-X1 (Android-N), MI-MA4 (Android-N))
I have tried Foreground notification which is killed after 5 to 10 minutes in Gionee mobile and instantly in MI mobiles,
Also tried Ignore Battery optimization worked partially in some mobiles and not worked in MI Mobiles
Tried alaram manager, not working properly after the app is cleared
Tried Work Manager, PeriodicRequests is not working, OneTimeRequests with delay is worked upt 10 minutes in Gionee Mobile (When i configured periodic requests, it executed nearly 6 times within a second and not executed after that)
Finally, I need a piece of code or a procedure which should run a background service (also foreground service) and not to be killed by system when the app is closed or cleared from background.Also please tell how to implement service has to be restarted if system kills the service
Need to disable "Battery Saver" process, to run your app continously .. In MI (My version is Redmi Note 8 Pro), navigate to Settings->Apps->Manage Apps->(select your app)->Battery Saver->(then set "No Restrictions")..
I have already red many posts about the subject, some of these works, but only on some devices. For example it works on my Nexus 4, Nexus 5 with cyaogenmod, but not on my Xiaomi Mi2s (jelly beam 4.1.1)
I tried setting it sticky, using alarm, and broadcast receiver on destroy. But neither of them worked out. When I kill the application it never wake up again. But on the same device Whatsapp works perfectly, and even if I kill it, it wake up again..
Is there any way to do it without use GCM?
Some of the link I already looked up:
How to restart a killed service automatically?
Service that repeatedly runs a method, after an amount of time
https://stackoverflow.com/a/21551045/627307
I'm not sure if it will solve the issue for you or not but it worked for me.
I'm running MIUI 6.6.10
Open the Security app provided by Xiaomi
Click on permissions -> Autostart
Here allow your app to auto start
WhatsApp and all famous apps work because Xiaomi white listed them and allowed them to autostart.