How to auto restart a service when killed? - android

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.

Related

How to Start forced stop application after phone reboot

I was trying to get to know about the possibilities of getting success of Starting up a forced stop application after the phone reboots. So here is the scenario:
My app will not have any activity. It will be a service only app where no activity will be declared. So my goal is when ever the phone restarts, it will start up the service automatically even though the service is force stopped before the phone reboots. I am using Google Pixel 6 with Android 12 rooted phone. So I have all the necessary permission to achieve the task. I have tried couple of examples from stackoverflow. none of them works. They works only if you keep running the app before the phone reboots. It wont work if the service is Forced stop before the phone restarts. Any help will be appreciated.

Android webRTC call in a Foreground Service

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

how the apps like whatsapp is running as the background service which are not killed by the android system

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")..

Not getting push notification when swipe from recent

I'm not getting notification when user swipe the app from recent. I've notice that behavior occurs in some specific device for instance I've tested it on nexus devices and HTC devices it's working fine but when user swipe the app from the recent in huawei and some samsung devices it doesn't show notification. I mean i don't get onMessageReceived() callback
After some research I found out that in these devices when user swipe it from recent it force stop the app (the process is completely killed) and I don't get any notification. In huawei they have protected and unprotected apps and if I add the app in protected list from the setting, I start getting notification because i this case it doesn't force stop the app. and I believe in samsung they have the same thing with blacklist and whitelist apps.
I've also tried some solutions like restart the service onTaskRemoved() callbacks or make your app START_STICKY. I've also tried WakefulBroadcastReceiver but nothing is working for me.
So my question is how can i get notification even if it gets forced stop, like it's happening when swiping app from recent.
An old question, but I think it’s important to know that HUAWEI has a feature called “power-intensive app monitor “. It kills every app that runs in the background for a long time unless user gives special permissions to it.
The path to do this:
Settings -> Security & privacy -> Location services -> recent location requests: YOUR APP NAME -> Battery -> uncheck Power-intensive prompt, App launch: Manage manually: check all three positions: Auto-launch, secondary launch, run in background.
I don’t know if there's a way to do this programmatically. I think the best way is to create a sort of help activity and explain the user what to do if application won’t work.

HTC autostart behavior

I have a strange problem with an HTC Desire, Android 2.2 and 2.3, Vodafone brand. My app consists of several activities and one sticky service. The service is launched on behalf of the main activity. In order to allow autostart, I'm following the usual autostart pattern (acting upon the BOOT_COMPLETED broadcast).
Now the situation on the HTC is as follows: After system boot the app is getting the broadcast and decides to NOT autostart. Several seconds later the app starts normally, although it isn't me, who launches the beast. This happens only if the app was active during the last power off phase.
So my autostart settings is not recognized, the HTC seems to launch any app with a sticky service, which is active on power down. I have double checked it with the "Music" app. Launch it, check in running services (you'll find it), reboot, it will be active again. Stop it, reboot, it is off.
Can somebody confirm?
HTCs have an "instant on" feature which seems just to store active applications on a file and then bring them back to the memory. Which, in fact, is not actually a reboot. It's more like Windows' hibernate function. So, check if the behaviour occurs when disabling "Fast boot" in Settings -> Power (or under Applications, depends on the Android version). Also confirm that doing Restart (not power off/on) actually sends BOOT_COMPLETED.
Unfortunately, if the issues is because of the fast boot stuff, there is no chance to get the BOOT_COMPLETED.
P.S. As it turns out it is, indeed possible to catch a fast boot. See: android register a permanent Broadcast Receiver
It is with <action android:name="android.intent.action.QUICKBOOT_POWERON" />

Categories

Resources