I am trying to run an android application continuously for testing purposes and noticed that the app stops after few hours. So I implemented an AlarmManager to start the activity every few hours, even with this I cannot run the app with out stopping.
I see the AlarmManger restarts the activity for few times and even it dies along with the App. Could someone suggest me how to achieve the functionality.
I really appreciate your help!
Thanks.
PS. I am testing the phone functionality using android like: cameraTest, ModemTest(playing audio, video files), Making Call, Bluetooth etc.
You could create a Service that restarts the app every x minutes or something. From the docs:
"A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. "
More here.
You can create a Service with a thread continously monitoring your application status(whether running or not) and it can launch a broadcast message to your application in case it is not running.
Related
I am working on an Android application that most it logic is done in background and basically analyzing the user activities (walking, running, in_vehicle etc)..
The ui has only 2 screens for basic setup and for giving permissions.
In the Application class onCreate (not Activity) the app register to ActivityRecognition api and gets the ActivityDetected events in a broadcast receiver which process the DetectedActivity and so on.
The app has also a boot complete receiver, after device boot, the receiver onReceive invoked.. This, causing the Application class to start, onCreate is invoke, the ActivityRecognition begins as described. This works perfectly!
So actually, the process starts in boot complete and nothing stops it..
Additionally, in the Application onCreate I send a firebase analytics Event (like AppStarted)
Also, when ActivityRecognition registration done I send another event (like ActivityDetectSuccsesfullySrarted)
Now here is the thing, in firebase I see that these events are sent about 20 times a day!!
Is there explanation for this?
This means that something, kills and recreate the process? Why?
Android terminates unused processes to free up system memory.
If you want a process to run for a long time on an off-the-shelf Android device, you will need to use a foreground service. If you are working with your own custom firmware, you could take other steps to try to keep your process around.
I'm trying to develop a mobile application which interact with ibeacons !
I'm developing this application for iPhone and android ! But i have problem !When i was developing iphone app, everything works,
i receive my notification even if my app is killed !
But on android i don't know how can i develop that !
If my application is on background, it works but if i kill my app, nothing happen !
Do you have an idea to run my code even if app is killed?
Thank by advance!
If you want run some code in background even if your application is killed you should use services. From google documentation:
A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
If your app runs in scheduled times you can use JobScheduler or AlarmManger. JobScheduler and AlarmManager wrok with services.
You can use Service. It will still working even if your activity is killed.
https://developer.android.com/guide/components/services.html
Android app that I am working on reads from near by beacons(devices) using bluetooth. It works fine when the app is in the foreground (tested it for 20 minutes). However, few minutes after app goes to background it stops reading.
I notice when app goes to background, onpause() method is executed; still my app reads for few minutes and then simply stops reading anything (when I manually bring the app to foreground, oncreate method is executed and app continuous normally).
Why is my app stopped reading few minutes after it went to background. My app is an activity and not service.
should convert the activity to service or
should I create intentservice or
should I create foregroundserive
I donot understand the difference between above 3 types of services and if any of them would help me.
Though slightly older threads, I reviewed Prevent that the app get stopped or paused by the OS and How can we prevent a Service from being killed by OS? and my app killed by android system when it running in background
But I am lost. Any discussion is appreciated
EDIT
As I understand from #davidgyoung answer, I have to write a service. I assume GUI portion of my app goes into mainactivity; then how I can ensure my mainactivity/GUI is still active in memory and was not killed by Android by the time service tries to broadcast/notify GUI
/EDIT
An Activity is not designed to run for long periods in the background. The Android OS will destroy activities that are not visible as memory is needed for other functions. While a Service is the proper alternative, even a service will be destroyed under memory pressure by the OS, so you still need to restart the service if it is killed by the OS and you continue to want to do beacon scanning.
All of these issues came up when we built the Android Beacon Library, and we settled on these solutions to keep scanning going:
Use a Service to scan for beacons in the background. It does not have to be an IntentService, but that is a reasonable option.
Use an AlarmManager to restart the scanning service 5 minutes in the future in case it gets killed. (This delay allows the OS to time to recover from a temporary need for extra memory.) If the scanning service is still running, just reschedule the alarm.
Register for OS level events (boot, power connect/disconnect) to restart the scanning service at a later time if the user kills the app with the task switcher.
All of this is built for you if you decide to use the Android Beacon Library (and we welcome contributions, too!) If you want to roll your own, you may want to look at the source code to see how these things were built. Feel free to copy and modify, too. That's the beauty of open source!
Full disclosure: I am the lead developer on the Android Beacon Library open source project.
I created an Android app and need to make it difficult for users to stop the main service that the app spawns during its startup process. This is for a rooted Jelly Bean 4.1.2 device. Here are some steps I've taken so far:
Installed as System App
Uses the Device Admin APIs
android:allowClearUserData="false" is included in the AndroidManifest.
The steps I've taken so far takes care of most normal ways a user would stop/disable an app/process; however, when you check the running apps list in Settings -> Application manager -> Running, users can still hit the 'Stop' button on the long-running service that was started by the app (see picture below):
Is there any way to prevent users from stopping the service here? Or what's the best way to restart a service when a user hits this stop button? I tried putting some code in the service's onDisable() function, but that function does not seem to be called in this case.
Any help would be appreciated!
As explained above does not have this option unless it is executed as root, but you can create an AlarmManager when starting your service that runs from time to time, the system will run if the service is not running, it will be created again.
Is there any way to prevent users from stopping the service here?
Having your app be a device administrator probably blocks this. It definitely blocks the "Force Stop" option.
I tried putting some code in the service's onDisable() function, but that function does not seem to be called in this case.
Since there is no onDisable() on a Service, this is not surprising.
This is a security app for an enterprise, so its expected to be continuously running.
There is nothing intrinsic to "a security app for an enterprise" that would require it "to be continuously running".
I want to perform action/event when application killed from task manager or any other app. Is there any to perform action when application killed. My application is running in background like service. If i terminate the application then main service stop . I want to start it again.
No, there's no reliable way to know if your application was killed by a another process. The whole point of "killing" an app is to terminate it as soon as possible, without letting it run any code.
== Do not actually use the following suggestions in production application. They are here purely as potential technical solutions, but in general are not a good idea for apps running on end user devices. ==
It might be possible to use IBinder.linkToDeath() from a secondary application, which acts as a monitor for your primary one. However, you will have to convince the user to install the secondary app as well. If you can do it, you could establish two-side monitoring between the two apps, and have one of them restart the other if the second is killed.
You could also attempt to set an alarm through the AlarmManager that fires every so often, to restart your application if it happens to be killed. However, if your alarm period is too big, you risk having certain period of time where your app is not running. And if your time period is too small, most likely your app will not be allowed by Google in the Google Play Store, and the malware app analysis on the phone (JB+) might kick in. Also, alarms that kick in too often will keep the device awaken, and drain the battery very fast.
If you kill some process, you just kill it, so it stops working immediately. There is no event sent to the application.
I looked for the same thing and the answer that i found is : NO, the application does not go to OnDestroy() or anything like that.