Execute code on location changes if app not running - android

I'm writing a small android app that need to execute some code when the devise change location.
I was wondering if it is possible to execute code on location change even if the app is not running (not background but stopped).
I mean, is there some system services that "wake" (or launch) my app (or a specific activity of my app) when lat/lng changes?
Until now I've create a locationManager, set some criteria, a provider and set the requestLocationUpdates. This is working but if the app is not running I got no update.

You need to create a Service that is notified of the updates. Services run in the "background" even when your app is not running.
This can get complicated since when a user is in your app, if you expect the app to update, then the activity will need to bind to the service.
Here is a tutorial:
http://www.vogella.com/tutorials/AndroidServices/article.html
And the official docs are good too:
http://developer.android.com/reference/android/app/Service.html
Be careful - services can be shut down by Android when memory runs low. Users can also "force stop" your app. Don't assume that it is ALWAYS running, but you can assume it runs most of the time.
And pay attention to the Service Lifecycle - it's different from and Activity:
http://developer.android.com/guide/components/services.html#Lifecycle

Related

Android 12 - Foreground service launch restrictions

I'm developing an SDK that needs to startForeground service from the background. Because it uses background location and Bluetooth-related works. If the application is killed, the monitoring is performing in the background. That's why I'm using the foreground service. There is a condition that starts the foreground service from the background.
Currently, my SDK using Service to handle this job. But Android 12 on-words it doesn't support to start service from the background.
I'm trying to start the service from the background the below exception throws.
ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false
How can I use WorkManager to fix this issue, all my handling is done by the Service class and how can I pass the Service object to Worker class and start this job inside the Worker class.
Actually, my project is based on beacon technology.
and the beacon signals are used to show different recommendations to the user.
In my current implementation, if the application is killed by the user,
and also accepts the foreground service, the SDK will be run in the background.
and detect the beacon and provide appropriate actions.
My implementation is that, if the application initializes my SDK with the foreground service "OFF"
Then sometime later, when the application is in the background and trying to start the foreground service from the background this exception throws.
The foreground service-related decisions are held by the server-side API. I'm periodically checking whether the server-side value is changed or not, and if the value is changed the changed action is reflected in the SDK.
There is no one in the world that can give you an answer. The idea of all these restrictions is that we as developers need to optimize our applications. So if this is not possible for you it means most likely that you need to optimize the way you do your work. For this to happen you need to provide more info of what exactly events you are receiving, what is exactly your use case, etc.
https://developer.android.com/about/versions/12/foreground-services#cases-fgs-background-starts-allowed
As you can see there is info about exceptions for:
Your app receives a Bluetooth broadcast that requires the BLUETOOTH_CONNECT or BLUETOOTH_SCAN permissions.
But there is nothing in your question saying that your use case might relate to this.
Also, I don't understand how the app might be killed, but you keep working in the background.
Also if you want to constantly do something - why there is an event when you are in the background. Just when the user opens the app - start the service and keep it going.
You can also just "hack" it and ask the user to remove you from battery optimization.
https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases
Earlier we were using Service to run background tasks. But, due to Android 12 - Foreground service launch restrictions, we will not be able to invoke Service for performing background tasks for Android 12+.
So from now on, from targetSdk 31, Service can be invoked only when the application is in the foreground. When the application is closed or when the application went to the background, invoking Service using startForegroundService will cause ForegroundServiceStartNotAllowedException.
So to perform background tasks, we need to use Worker instead of Service. Please refer to this answer to get an idea of how it is implemented. Hope it helps. Also, refer to the below links to get a high-level overview of what changes needs to be done.
Android 12 Behavior Changes
Work Requests
According to the official docs, if your app does one of the following, it should be able to start an FGS:
Your app receives a Bluetooth broadcast that requires the BLUETOOTH_CONNECT or BLUETOOTH_SCAN permissions.
or
Your app receives an event that's related to geofencing or activity recognition transition.
Those two seem like pretty good candidates for your use-case, at least how I understood it.

Android Activity has destroy after few minutes when app is pause

I create an application which get location and send data to server in 5 min intervals. But when my app is paused after 2/3 min app is destroyed. I want to running service till I destroy activity. Please give some suggestion.
There are so many reasons lets your app crashes when going background. You should give some code.
Your problem, I think your device is Android O or later. In Android O, if you create a BACKGROUND service, the app will be destroyed in the background, you should use FOREGROUND service instead.
Reference: https://developer.android.com/about/versions/oreo/background
If you intend your application to work even when the app is not visible to the user, use a Service, which runs in the background.
Using a basic Service is just as easy as creating an activity, give it a try.
Also, posting relevant code that shows what you've tried could also help.
Since this app runs in the background, be sure to optimize usage of location services so as to save the phone's battery.

keep service running in background in android

I am developing a chat application in android . and need to keep service running
even after exit from application .
I am usin
return START_STICKY;
in onStartCommand() of my service .
but because of limitation of services in android oreo , service will destroyed after seconds when exit from application.
So far users lost new messages notifications.
I can not use Fcm beacause of local networking and no access to internet.
And I can not use ForegroundService . (because Of Employer's request to not showing any notification) .
When I checked running service in android mobile setting , there are some
apps that their service not killing like Es file explorer , Zapya , ...
How they keep their service running without foreground service .
And What should i do .
Show in blow image , some apps services are running without any notification .
Based on the documentation:
The system distinguishes between foreground and background apps. An
app is considered to be in the foreground if any of the following is
true:
It has a visible activity, whether the activity is started or paused.
It has a foreground service.
Another foreground app is connected to the app, either by binding to one of its services or by making use of one of its content
providers.
Reason Es FileExplorer can do could be (its just my opinion) following:
Es FileExplorer (is quite cheeky when it comes to taking advantage of some loop holes) have several content providers but one provider, FileProveders which is some how manages to have com.android.providers.settings connected to it. I guess this connection makes it foreground. They virtually have all the possible intent-filter registered for almost all the scheme. Anything you try to share or access, could trigger them some or the other way which keeps its process in use (you can just click on the details and you will find LocalCService of app running).
But for your app:
If you can't use FCM, ForegroundService and can't have visibility to user, then only option is to perform task periodically. You can use WorkManager. The only limitation is minimum duration for scheduling is 15 minutes. Refer to my answer for scheduling work with WorkManager and WorkManager vs Service for usage of WorkManager.

How to keep an app running continually with GPS on?

I have an application that is required to log the user's location every couple of seconds, the entire time it is on.
On does not mean in the foreground,or that even the Android's screen is on.
What is the suggested way to accomplish it?
I've heard of background and foreground services, and also saw something about Jobs. I also saw this was possible with a WakeLock.
I am not sure what is the best method of choice.
The need to conserve Battery life is of course an issue.
You should use Service, you can choose that the service wil keep running even after your app was closed.
YET if the device is low on resources it might close your service.
you can use FOREGROUND service if you want to minimize the chance android will close your service.
inside the service use LocationManager / FusedLocation to get the location every X time.
you set it up with LocationListener so everytime it set onLocationChanged you upload to firebase.
Useful links:
Make your app loaction aware
Services

How to prevent a user from stopping an Android service?

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

Categories

Resources