Internet Connection Change Job Service need to schedule everytime in activity? - android

I was reading android documentation for register a listner on network change for android pie and below versions.
I found out , for version above lolipop i can run a job service which monitor network changes.
So my question is
If it monitor network changes irrespective of the app is running or not. how do i take care this job service in my app activity life cycle.
curently what i have done is i am scheduling this job in onResume and stopping in OnDestroy in my activity. but i want it run on every network changes

Related

How call webservice in every hour if internet connected to device

I want to call web service in the background when internet is connected. If it is connected I want to call in every one hour if internet is turned off then stop the service. How can I? It needs to work for all versions from 16 to 27.
There is broadcast in android which notifies you if network status changed. In there you can register alarm manager for a period of one hour and check if the internet was connected then call the web service.
If broadcast informed you the network was disconnected then you can cancel your alarm manager.
Here is documentation for listening to network status change:
https://developer.android.com/training/monitoring-device-state/connectivity-monitoring
and here is good tutorial about using alarm manager in android
https://en.proft.me/2017/05/7/scheduling-operations-alarmmanager-android/
also google introduced workManager which does this work for you in the very simple way
The WorkManager API makes it easy to specify deferrable, asynchronous
tasks and when they should run. These APIs let you create a task and
hand it off to WorkManager to run immediately or at an appropriate
time. For example, an app might need to download new resources from
the network from time to time. Using these classes, you can set up a
task, choose appropriate circumstances for it to run (like "only while
device is charging and online"), and hand it off to WorkManager to run
when the conditions are met. The task is still guaranteed to run, even
if your app is force-quit or the device is rebooted.
here is link to documentation :
https://developer.android.com/topic/libraries/architecture/workmanager

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.

Android O monitoring network changes

What is now the recommended way to monitor connects and disconnects to and from WiFi networks?
Ordinarily, I would register a BroadcastReceiver in my manifest and listen for state changes.
Android O is going to make this impossible:
Apps cannot use their manifests to register for most implicit broadcasts (that is, broadcasts that are not targeted specifically at the app).
What is the next best (least battery consuming) way to accomplish this same task?
My initial assessment is to create a Service that will monitor network state changes. I don't like the idea of running a permanent service for this purpose alone.
Option 1: You can register the BroadcastReceiver in Java code, for instance, in an Activity's onStart() and unregister in onStop(). This works only when your Activity is alive, of course.
Option 2: Use JobScheduler. You can schedule your jobs based on the available criteria in the JobScheduler like network connection availability.
My initial assessment is to create a Service that will monitor network
state changes. I don't like the idea of running a permanent service
for this purpose alone.
Even this is not so simple. Your Service will be killed if the system finds it idle. If you don't want the Service to get killed, you have to start a Foreground service, but the catch is that a requires a notification to be shown to the user.
Check this library for this
https://github.com/Ahmed-Abdelmeged/Networkito
Work with rxjava and Android Architecture Components and normal usage

Create a Long Running service

I need to create a service that runs alongside the android app,irrespective of which screen of the app the user is on.
The app is a chat application so when the device is offline the service should queue up all the messages that are being sent offline and when the device is connected it should sync all messages.
I have written code for the job scheduler to sync data automatically when the device is online but while the app is active i would like to handle this manually.
Creating a Long Running service.
Operating system still can terminate the service in low memory and possibly other situations. There are 2 ways to overcome this:
If you are implementing the service, override onStartCommand() and return START_STICKY as the result. It will tell the system that even if it will want to kill your service due to low memory, it should re-create it as soon as memory will be back to normal.
AlarmManager .A system service, which will execute actions periodically. That will ensure that if your service will be terminated, or even the whole process will die(for example with force close) - it will be 100% restarted by AlarmManager.
Thank you.
You can do this by simple following steps:
Create Simple Service and after first launch of app just start at splash screen.
In Service after getting one data you can call another request.
After that you can create one broadcast action globally which will always call every network changed.
At background you can sync again data and saved it to shared preferences or as per your your requirement.
For interval you can also using AlarManager.
A part from this you can simply create Service using JobSheduler in this you can assign job and time as well.
Refer link :
https://developer.android.com/reference/android/app/job/JobScheduler.html
Hopefully this logic will helps you.
You have to use a intent service with sticky instead of service for this which will be executed in a queue and do your work. And since it is a intent service it will be started automatically after sometime, when system kills the service process.

Service lifecycle in Android

I have a service in my Android application that needs to continue listening for location updates after the user has exited the application (the implications of this on battery life are a separate matter).
I'm not sure that I have correctly understood the lifecycle of a service in Android, outlined on this page:
http://developer.android.com/reference/android/app/Service.html
I believe if it returns START_STICKY in the onStart() method then the service will continue to run after the main application has quit regardless of whether or not the service is running in its own process. If the service is running in the same process as the rest of the app and I have understood correctly, the main app's process is kept alive after the app exits, just to run that service. When the app starts again, it will run in the same process as the service, which is still running. If the system gets low on memory, Android may decide to kill this service.
Secondly, I believe it is OK to run the location listener listening for GPS updates in the same process and indeed same thread as the rest of the application and it will not block when waiting for updates from the GPS.
Have I understood correctly?
You have understood correctly.
If the system gets low on memory, Android may decide to kill this service.
If you want it to be persistent you can create persisitent service (but it needs to be a system app / service).
Please use AlarmManager and an IntentService, so your service does not need to be in memory except when it is doing meaningful work. This also means Android is rather unlikely to kill your service while you are in memory, and users are unlikely to kill your service because they think you are wasting memory.
For your location Listener:
Use the Location Listener implemented in a service.
Start listening the GPS when the service starts and remove the GPS listener when the service stops.
Start this service when you wants to listen to GPS(every 10 minutes for example).
This is cleaner than having a service you try to continuously run and checking for Location changes.
Secondly, I believe it is OK to run the location listener listening for GPS updates in the same process and indeed same thread as the rest of the application and it will not block when waiting for updates from the GPS.
You do not need to setup a AsyncTask or background thread for this. Also understood correctly.
The actual life cycle of Service is described here by image. You run whatever service in android the life cycle if always follow like this.

Categories

Resources