I have seen many pages about this problem but didn't help me.
I have a service that should run in background even when the screen is off.
but some android devices (like many huawei phones) have battery care stuff that can kill service when screen is off, and android 6 has that option (keep running after screen off) but user should give that permission himself.
I see that telegram service get killed too, but how whatsapp service doesn't stop even when the screen is off.
this is my service in androidManifest
<service android:name=".myservice" />
is there any option in manifest that can keep service running when the screen is off? I can't use sticky-services because it runs over and over after screen turns on but my service should start once!
or any other solution for this problem?? thanks!
Related
When I used foreground service then when I kill the application then foreground service is automatically killed but I want to alive foreground service when the application is killed. this issue appeared in android 10 and android 11. how to solve this issue.
You can't do that as you should not be able to keep alive a foreground service after the application is killed.
You should only use a foreground service when your app needs to perform a task that is noticeable by the user even when they're not directly interacting with the app. If the action is of low enough importance that you want to use a minimum-priority notification, create a background task instead.
If you what to have some background service working, that will be possible.
You can find some useful information here :
https://developer.android.com/guide/components/foreground-services
https://developer.android.com/training/run-background-service/create-service
It should not get killed. When starting foreground service you need to create notification too. You have done that, right?
If you done everything right there is possibility that you have xiaomi phone. Xiaomi deletes everything when app killed. You need to add specific intent protection...
I'am using Agora.io video call for my application, it is working fine both audio and video, but when I put the device in sleep mode, after some minutes the application probably goes to sleep mode and I'am disconnected from the call channel until I wake up my device again.
I searched the Agora.io documentation for any issue but without success.
Has anyone an idea?
SOLVED
I used a ForegroundService that set a Persistent Notification and I was able to ask the service to keep the Camera and Microphone active
<manifest>
...
<service ... android:foregroundServiceType="microphone|camera" />
</manifest>
I am trying to create a locker application to show pin view when the user enters to defended application. I created a service that runs and tracks the current foreground application. However, on devices with android version 10 activity is not starting from my service. I know that from android 10 there are restrictions to start an activity from the background, however, there are exceptions either. The activity can start from the background when the application is in the foreground, but it won't help in my case. So the question is how can I start the activity from service to show pin view?
P.s: I tried it with window manager but again in higher android versions there is an annoying notification that can open settings do dismiss overlay permission. Also, I saw a working "app locker" application in the play store, so I believe there is a way.
From android 10 foreground service will work only if u start the service while the app is in foreground. If your app is in background you will need background location permission which has to be requested separate after you gain permission for coarse and fine location. Also if your app is in testing mode and not in Play Store and u update your device to android 10 a fresh install of the app is needed.
There is no point in using a foreground service if u want to start that service from background. Just get background location and stop using foreground service.
IN bound service when I lock the device after the 70- 80 seconds service stops I am testing on realme 3 pro and one plus 5 both having API 9 PIE. How to keep service running until the app is in recent activity. Even foreground services are also stopped. Thanks in advance.
Realme, Oneplus, Samsung, Xiaomi, Huawei and a few other manufacturers have their own layer of "Battery Saver" or "Security" that kills or restricts background running apps in order to improve security/battery backup, unless user white-lists your app in the relevant device settings.
What i found out about Xiaomi is that they white-list well known apps like Facebook, Whatsapp, etc. But you cannot request Xiaomi to white-list your app.
These manufacturer apps simply terminate your app process. So your app should behave in such a way that termination at any moment should not create inconsistent state.
Also, you can always show a popup to user after installation asking him/her to go and white-list your app in battery saver. In Xiaomi, the user will need to select 'Do not restrict background activity' and also enable 'Auto Start' for your app. As far as i know, there is no other solution.
Conclusion: Visit https://dontkillmyapp.com and see how the apps are whitelisted for your OEM. Once whitelisted, it will work as expected.
But out there in real world, it will get killed and users won't manually whitelist it.
Custom Roms have it's own security apps and it close the app service when u remove it from recent or after few minutes.
To keep running your app in background you have to enable autoStart from settings
Refer this : How to fix the issue that the activity is killed automatically after some minutes in newly version as like vivo and oppo
Facing same issue from long time, Try to add FCM in your application and start service from it directly or else set alarm and startservice.
In some cases above solution work. i have added above solution in 4 project but succeed in only 1.
Issues which I have found while overcoming problem with firebase.
Firebase Notification coming to app but Background service not starting
Firebase Notification Not coming to application
Firebase Notification coming and service starting with Broadcast Receiver (Sometimes
service not starting directly from onMessageReceived() method of Firebase so i
decided to set broadcast Receiver ) but sometime its stop working.
Issue which I have found without implementing Firebase
If we set background service to run after every 5 mins than for 1-2 hours its
running on 5 mins interval but after 2 hours time gap increased and service runs on
15 mins of interval and in some devices its run on 20-30 mins of interval(i found
this in right time because phone may goes in ideal mode).
As mention in your question, sometimes any of above implementation doesn't work.
Tip : What I found in system alarm log ?
I have set alarm for 2019-08-08
11:07:00 which not fired and current time is 2019-08-08 11:10:00 but its showing in
pending in system alarm log.
You can check system all Alarm set by your applications
D:\SDK\platform-tools>adb shell dumpsys alarm
Is it possible that if phone's screen turns off completely but phone does not go to sleep?
I have an app which do some things when device goes to sleep. I registered broadcast intent: Intent.ACTION_SCREEN_OFF
On my device app works fine ( HTC Desire, cyanogenmod ). When screen turns off, my app is activated.
I tried app on another phone (HTC Wildfire S, offical gingerbread ), and here is the strange part. When screen goes off my app is not activated (sometimes it is, like 10%). So i connected it with my LOGCAT and i wanted to see if device actually goes to sleep when screen is turned off, and the thing is that when is connected with my PC, device ALWAYS goes to sleep and my app is activated.
On emulators from froyo to jelly bean app works perfect. Is problem in my app?
From my testing of Screen Status broadcasts, I have learned that different manufacturers handle the screen off intents in different ways. Can't really depend on the phone.
You might want to use PowerManager to detect status of the device and take appropirate actions in onPause()
public static boolean isDeviceScreenOn(Context argContext) {
PowerManager pm = (PowerManager) argContext.getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}