HTC autostart behavior - android

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" />

Related

How to deal with Huawei's and Xiaomi's "battery optimizations"

I have an app that listens for notifications via NotificationListenerService. My app survives both the Doze mode and app stand by on almost any phone (if properly set-up e.g. exception or a foreground service).
Then come the Huawei and Xiaomi with their own modifications of Android to "improve battery life". These can mess with background running apps so badly that after a week or two the app must be reinstalled. Even clearing data will not "unfreeze" the app. Even exceptions to their own "optimizations" does not reliably fix this. Only completely disabling all their optimizations e.g. enabling "high performance mode" might fix this. But users do not want to disable these options.
How do you deal with these cases? And how can we stop manufacturers to stop messing with core Android? Not to mention the funny ANRs & crashes in Zygote :(
Then come the Huawei and Xiaomi with their own modifications of Android to "improve battery life".
Yes 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 can mess with background running apps so badly that after a week or two the app must be reinstalled. Even clearing data will not "unfreeze" the 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.
Let me know if you find one ;)

Prevent my app to sleep when device goes to sleep mode

I have written an app that should be running continually event if device goes to sleep mode.I have tried many things like services (with startForground also) , alarm manager and persistant="true", but when device go to sleep (by user or by device) , my app is terminated.and even PowerManager does not work too.
There is an option in device setting that is 'Keep running after screen off' (in huawei device : setting > app > my app options > battery > Keep running after screen off). So when this option is active , the app is running for ever.I want implement something like this in my app programitically.
I saw some apps that implemented this , but I dont know how.
So how can I do that?
Use ForgroundService.
If you check your device applications with an app manager, you can see that some of them using a sevice that is always running. even if you stop them, some of them start working again.
see more about services behavior in android developer.
and search for "keeping a service alive".
On some devices (Huawei, Xiaomi, LG) there is a special setting that contains a list of apps that are allowed to run in the background. It sounds like you are talking about trying to accomplish the same thing. This cannot be done on those devices. On those devices, unless your app is added to the list of apps that are allowed to run in the background ("Protected Apps" on Huawei), the Android framework will not restart your app if it is shutdown. There is no way for you to programatically add your app to this list, and there is no way for you to work around this feature.
Certain apps need to keep the screen turned on, such as games or movie apps. The best way to do this is to use the FLAG_KEEP_SCREEN_ON in your activity (and only in an activity, never in a service or other app component).
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

How to auto restart a service when killed?

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.

BroadcastReceiver not working when I kill my application

I have noticed than whenever I manually kill my application by longpressing the back button of my cellphone my broadcast receiver stops working. The receiver is in charge of displaying a notification every time the user hangs up a phone call and the same is registered in the manifest.xml.
Is this the normal/expected behaviour? I thought the receiver should continue to work even if the user decides to kill my application... Is there a way to prevent this?
Thanks.
Edit
Here's the manifest entry for the receiver:
<receiver android:name=".BroadcastReceivers.CallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
There are ~7 billion people on the planet. Only you know what you mean by "kill".
The symptoms that you are describing, though, are consistent with a "force stop". A user normally force-stops an application by going to Settings, finding your app in the list of installed apps, and tapping on the "Force Stop" button for your app. There are some devices and firmware builds that make "Force Stop" more readily accessible than this -- such devices and firmware builds were written by drooling idiots IMHO.
If your app is force-stopped, your code will never run again, until something uses an explicit Intent to start one of your components. Usually, the user does this by tapping on your app's icon in the home screen's launcher. Until the user does this, your BroadcastReceiver will not work, and there is nothing you can do about it.
Rather than using some on-device feature to "kill" your app, try terminating its process via DDMS. If your app continues to work in that case, then however you elected to "kill" your app before is doing a "force-stop". Merely having your process be terminated, such as due to low memory conditions, should not prevent you from receiving future broadcasts.
I know that some devices (like my ASUS) are deleting static receivers when you stop an application, yours is probably one of those.
The only thing you can do is trying with emulator or other device.

Prevent that the app get stopped or paused by the OS

I'm developing and Android application on CodenameOne that needs to send a web request every 5 minutes even when minimized. How can I achieve this behavior in order to prevent that the request get stopped or paused by the OS?
You cant do that from the activity, you'll need to create background service.
http://developer.android.com/training/run-background-service/create-service.html
Use AlarmManager to set up your every-five-minute operation. Have it trigger a BroadcastReceiver, which in turn passes control to my WakefulIntentService (or your own IntentService that handles the WakeLock, that you will need so that the device stays awake while you do your work). Have the service do the "web request".
This is still not 100% guaranteed:
The user can Force Stop you from Settings, in which case your alarms are gone and nothing will happen until the user manually runs your app again
The user might block alarms from firing on certain devices, like various SONY Xperia models, that offer "stamina mode" or the equivalent
However, it is the best that you are going to get, short of rolling your own Android OS version.
The other guys answers are correct that you need to create a service but they somehow ignored the mention of Codename One.
Under Codename One you need to create a native directory for android and just place the service source code there (just use a blank service class that doesn't really do anything). Then you need to add to the build arguments the option android.xapplication where you would state the service XML attributes.
Having said that what you are trying to do is VERY wrong and you shouldn't do it in Android! You will drain the battery life from the device in no time and the service will be killed by the OS eventually (since it will be a battery drain). The solution is to send a push notification to the device to wake up the application.
In Android 9 and newer you can prevent your App falling asleep with a battery setting.
Long click on your App -> App info -> battery -> optimize battery consumption
Here add your App from the list.Hint: maybe the menu entries have a different name, depending on your phone.

Categories

Resources