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.
Related
An application, let us call it Application A, that is installed on my phone produces very interesting data. I am not in control of Application A, but the developers were so kind as to broadcast the interesting information locally, so that other applications (like the one I am building, Application B) can work with the data produced by the other application.
I am registering a BroadcastReceiver via the following code in the onResume() of my MainActivity:
registerReceiver(new CustomBroadCastReceiver, new IntentFilter("com.intent.filter.DATA"));
This works perfectly fine, until my application is either force stopped or stopped by Android (presumably to preserve power/free up memory?).
However, Application A produces data all day and all night long. Based on this data, Application B calculates further results and is supposed to sound an alarm, as soon as the readings go in the wrong direction. It is imperative that the BroadcastReceiver in Application B can sound an alarm at any point in time.
What is currently best-practice to keep the BroadcastReceiver alive as long as possible (maybe even surviving a force stop (swiping away) of the application by the user)?
Edit: I found out, months later, that my Samsung phone had put my application in a power saving list of apps that it will forcefully and regularly kill to preserve power. Make sure your application is not in a similar list on your own phone.
You need to register your receiver in your manifest, not programmatically. See:
https://developer.android.com/guide/components/broadcasts#manifest-declared-receivers
However, if you target Oreo+, you won't be able to do that anymore. App A would need to explicitly call out your package for the broadcast to be received by an receiver declared in App B's manifest. You can read for about that here:
https://developer.android.com/about/versions/oreo/android-8.0-changes#back-all
This isn't a great way for your apps to share data (and hence why Google are phasing it out). It's inefficient to have to load your app into memory each time something happens in another app. Imagine tens or hundreds of apps doing the same thing for all manner of broadcasts, and what that means for battery life.
A better way would be for App A to expose the data in a content provider, and your app to wake up (infrequently) and pull the data.
You need to register your BroadcastReceiver in the app manifest file AndroidManifest.xml
<receiver android:name=".CustomBroadCastReceiver" android:exported="true">
<intent-filter>
<action android:name="com.intent.filter.DATA"/>
</intent-filter>
</receiver>
The system package manager registers the receiver when the app is installed. The >receiver then becomes a separate entry point into your app which means that the >system can start the app and deliver the broadcast if the app is not currently running.
https://developer.android.com/guide/components/broadcasts#manifest-declared-receivers
Which means that the system will launch your BroadcastReceiver even if you app is closed, unless your app was force stopped by the user https://stackoverflow.com/a/9240705/1905761
You need to keep in mind though that some restrictions are applied starting from android 8.0 (Oreo)
https://developer.android.com/guide/components/broadcasts#changes-system-broadcasts
Im using Sumsung GT-S7710 android version 4.1.2 . When I force close my app from app list in my phone manually - separate service process becomes dead. I can see it in studio. And trully it doesnt work any more (I used to check it with shedulled launching of notification sound). But in many places over the net people say android:process=":remote" option will prevent my service from being killed, but it seems not working for Force Close case. Any suggestions appreciated. Thx in advance.
<service android:name=".ActService" android:process=":remote"/>
When I force close my app from app list in my phone manually
What you appear to mean is that you are going to the Settings app, to the Apps screen within there, finding your app, and tapping the "Force Stop" button.
separate service process becomes dead
Sure. All of your processes will be terminated if the user force-stops the app through Settings.
But in many places over the net people say android:process=":remote" option will prevent my service from being killed, but it seems not working for Force Close case.
Correct. It is not supposed to work for that case. Where a separate process may help is if the user swipes your app's task off of the overview screen (a.k.a., the recent-tasks screen).
When android device power on, is it possible to load one specific application without launching home screen?
How to make this change?
Where to change code
Setting permission as boot_completed works but before my application is loaded, the homescreen is shown for 5 seconds. How to disable android from showing any homescreen launcher before my application
This mode is called kiosk mode, and android has provision for running devices in Kiosk mode, you can refer to this guide for the complete set up. Basically what you do is set up your app to listen to the RECEIVE_BOOT_COMPLETED broadcast and disable all actions such as back button home button minimize buttons etc. Also don't forget to set up an exit mechanism. Just follow the guide and you should be fine.
In your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And add Intent filter:
<receiver android:name=".BootReciever">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
And then Now you can start your application's first activity from onReceive method of Receiver class:
https://stackoverflow.com/questions/10428510/how-to-start-launch-application-at-boot-time-android
For more information you can check this url How to start/ launch application at boot time Android
if your device has been rooted, uninstall system applications on your device, but be careful that every app can not be uninstalled. install "Root-Uninstaller-Pro-8.3" app and do this. you must uninstall important system apps one by one and any time restart your device to see result until Achieve goal. if you uninstall an app that make your device Messed up, you should flash your device to restore apps and retry uninstallations. by this uninstallations, your boot time become too short and that 5 second will be disappear. good luck!
I'm making an Android app and I would like to know how to hide its icon and title from showing in the menu. Everything I've found so far on the internet is hiding
<category android:name="android.intent.category.LAUNCHER" />
in AndroidManifest.xml.
If I do that it doesn't start after installation! Another important note: I need my app to ALWAYS be running, even after restarting the phone.
but if i do that i can't start my app after installation!
Correct. Effectively, as of Android 3.1, you must have a launcher icon, at least at the outset.
i need my application to be ALWAYS turned on, even after restarting the phone (turning it off and on).
I have no idea what "ALWAYS turned on" means in terms of an application. I am going to assume you mean "ALWAYS running".
In that case, this is not possible. Users can and will stop your app by any number of means, such as task killers and the force-stop option in Settings. The Android OS will automatically stop your app if it is hiding in the background for too long. It is considered poor form to have some piece of your application (a service) running constantly -- very few applications need this.
Moreover, please bear in mind that the combination of "I need it always running" and "I do not want it to appear in the launcher" sounds very suspicious, almost as if you are trying to write something that the user might not want to have running.
Then you would need some way to start a service. Or figure out a meaningful way you can actually start up your application without user interaction, there are tons of options.
There are lots of Intent defines for receiving broadcasts through which you can start you activity for example
ACTION_BOOT_COMPLETED
ACTION_POWER_CONNECTED
for further detail check this link
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" />