Implementing BroadcastReceiver inside JobService - java

I am working on an application that uses JobService. Now, in my app i want to reschedule the Broadcast receiver that is working in the background. So, i want to know if i can implement BroadcastReceiver inside JobService so that it can restart after several times.

Yes it is possible. If it's background service, use PowerManager and WakeLock to keep CPU running while you you restart your service. Btw why you need to restart it?
EDIT:
if you want to keep alive your service return START_STICKY in your onStartCommand()

Related

How to get broadcast receiver when an app is killed manually?

I am developing an app which records unlocks of mobile. But I am having problem when my app is manually killed i.e. when you remove app from recent apps. How can i record broadcast even when app is killed?
I am using ACTION_USER_PRESENT for recording unlocks.
You'll need to create a Service and inside this service create the broadcast receiver that you need.
This answer Certainly will help you : Implement Broadcast receiver inside a Service

Will Application Class OnCreate be called if a Broadcast is recieved

I am using a JobIntentService perform some task after the task is ended I send a Broadcast which is also listened by one of my Activity.
Suppose in a case where the activity is closed by Android OS to free up some memory then if the Broadcast is sent from the JobIntentService will the OnCreate of the Application also be called?
I don't know if answer marked correct is still correct. On a phone running Android P, if you have a manifest declared broadcast receiver and an Application class defined in your <application> node, the onCreate() in your Application class is called before the onReceive in your broadcast receiver. I see no reason to believe other versions of Android would behave differently.
NO. It will never call the applications OnCreate() method (is called only during a cold start).
A cold start refers to an app’s starting from scratch: the system’s process has not, until this start, created the app’s process. Cold starts happen in cases such as your app’s being launched for the first time since the device booted, or since the system killed the app.
About BroadcastReceiver Lifecycle:
If your receiver is registered in activity then it's lifecycle is the activities lifecycle. So your receiver will not be able to listen once your activity gets destroyed.
If your receiver is registered in application then it's lifecycle will be applications lifecycle and it will be able to listen to the broadcasts as long as the application is not destroyed.
JobIntentService & Receiver
When you start a JobIntentServie (from a receiver) then your job intent service will not get killed by the OS as long as there is active jobs going on (please note that there may be a time limitation, to know more: How long is the "JobService execution time limit" mentioned in Android's JobIntentService docs?).
Now if your activity is destroyed in the meantime then your broadcast receiver will not listen to the broadcast because it's lifecyle has ended, if you don't unregister the receiver yourself, the system will kill it as system considers the BroadcastReceiver to be no longer active.
So if you want to listen to a broadcast as long as your application is not destroyed, you should register your broadcast receiver in applications onCreate() method.

How Facebook and Truecaller always running its process and services in background even though it's killed by ram cleaner?

I want to develop an android app in which its service and process needs to run always in the background. Can anyone help me to implement like that?
In android, whenever a Service is killed, the OnDestroy() method is called.
You can restart the service here, by sending out a broadcast to a BroadcastReciever class which you will have to create. In this class, you will override the OnRecieve() method and then restart the service inside this method. You will also have to register this class in you manifest file.
Broadcasts: https://developer.android.com/guide/components/broadcasts.html
Services: https://developer.android.com/guide/components/services.html
To summarize,
Create a class that extends the BroadcastReciever class, and override the onRecive method.
In theonRecieve method, restart you service (presumably by a
ServiceManager)
Send a broadcast to this, when you service is being destroyed.

Is a START_STICKY service restarted after application killed itself

So if I understand right, the difference between START_STICKY and START_NOT_STICKY is that the first will be restarted by the system, in case it kills it.
Does anyone know whether this also happens when I kill my application using Process.killProcess(Process.myPid())? Or does the system only restart the service if the service was actually being killed externally, not from within the application process?
Not always, you can't depend on START_STICKY for continuing running the service. Better way is to use startForeground for continuing running or if you want to repeat specific task then schedule your service on specific time. Also, manage it when the phone is Restarted as well.
Notification.Builder builder = new Notification.Builder(getBaseContext())
.setContentTitle("");
.setContentText("Your content text");
startForeground(1, builder.build());
Yes, the START_STICKY service is restarted after the application is killed by the Android System.
Until and unless you call stopService() method from an app component or stopSelf() from within the service, the service will be restarted if the app is killed.
But you can always make the service run using startForeground() method, in which you will have to show a notification in the status bar for your service. If you create a service using this method, your service will run at the same priority as an active activity. This means that it is highly unlikely that your service will be stopped, and no restart would be required.

Running Android App in the Background after "killed"

i'm developing an Android App for mobile sensor monitoring (accelerometer, gyroscope etc). The application is build on a class that extends from BroadCastReceiver and has communication with a service (a class that extends from Service) which is responsible of recording the sensor data when some android events occurs. When the app is running in the background works nice until I kill the activity (in this activity the user can adjust some params). I want to continue recording after the app is killed, how to fix it? Thanks!
i think you forgot to add service into your application you need to start service from your activity and then register broadcastreciever with IntentFilters after that your application will never be stop might be you will interested in this answer
You can use both of this framework to schedule your service to start at the time that you want. Like on every hour or so
http://developer.android.com/reference/android/app/job/JobScheduler.html
http://developer.android.com/reference/android/app/AlarmManager.html
but remember that if someone will go to task manager and kill your process there is no possibility to avoid that.
cheers
Solved with return START_STICKY (onstartcommand() of the service) and PowerManager inside the service to prevent the Android CPU from going to sleep. Thanks to all!!

Categories