In my android app, I have a Service
that starts when the app goes in background. OnStartCommand
begins long running task that analyzes and checks device and app status.
Now, I am preparing the app for Android O. First issue that i faced is Services
, I rework them to JobServices
.
Now I am facing another issue. When user removes my app from application stack JobService.OnTaskRemoved
doesn't get called.
Before, when I used Service
calling of Service.OnTaskRemoved
worked fine for me.
Now I see only way. I need My old Service
for handlingService.OnTaskRemoved
and new JobServices
for executing task.
Am I wrong? May be some one can give me good advice?
You are implementing the wrong concepts.., which creates problem.., To tackle it .. you are again implementing wrong things.. Please offload all...
In my android app, I have a Service that starts when the app goes in background..
The same service can be started whenever App is opened first time... On acquiring all runtime permissions. As :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
context.startForegroundService(new Intent(context, YourLongRunningService.class));
}
else
{
context.startService(new Intent(context, YourLongRunningService.class));
}
OnStartCommand
begins long running task that analyzes and checks device and app status.
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d("RUNNER : ", "\nPERFORMING....");
return START_STICKY;
}
Above single line tells android to keep alive ALL THE TIME UNTIL DEVELOPER STOPS IT ON HIS OWN VIA CODE STOPSELF Even removed from background
or from recents
; It gets re-created automatically by android os
Then where should i write the code.... ??
Wait... its too early to code yet....!!! patience
begins long running task that analyzes and checks device and app status
Your question is un clear and in my brain there is a gradle error ... I can not resolve your symbols
:
But i know that these your broadcasts definitely .. And you need to implement broadcast-receivers... to receive it...!!
I will implement Broadcast receivers... Its too easy...
Wait... Android O do not allows you to implement many broadcast receivers from static receivers like we does... And even we are not allowed to call that receivers on specific intent-actions from manifest.xml
Implement runtime receivers in your above created service YourLongRunningService
in onCreate
like :
IntentFilter myFilter = new IntentFilter();
myFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
this.registerReceiver(mCallBroadcastReceiver, CallFilter);
CONNECTIVITY_CHANGE is an example and it would be your intent action / broadcast you want to listen for...!!
What is
mCallBroadcastReceiver
and all...
These are runtime registration of receivers... and needed to be unregistered from onDestroy
like :
@Override
public void onDestroy()
{
this.unregisterReceiver(mCallBroadcastReceiver);
super.onDestroy();
}
No.... No... No.... I do not want to un-register it... I want it always working..
Our service is START_STICKY
Even if it is destroyed; gets started automatically and registers mCallBroadcastReceiver
in onCreate
again..
Where is the receiver then....???
In this case mCallBroadcastReceiver
is receiver defined in class area where we declares the variables and constants :
public class YourLongRunningService extends Service
{
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
String NOTIFICATION_CHANNEL_ID = "1";
private BroadcastReceiver mCallBroadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
// All your receiver code goes here...
}
};
// your constructor
// your onStartcommand
// your ondestroy
}
What notification Manager is doing here....???
In and above O you can not run foreground service without proper Ongoing
task notification... It will go in your onCreate
which will call Startforeground with the notification to start this service as a foreground service
What below android O...?
It works below Android O too... Just call with normal startservice its code it given on very start...!!
Where is my long running code goes then....???
From receiver receive broadcast you want and start a intentservice
or a job
or a alarm class or whatever you want...
I will make a simple class which takes
context
in constructor and defines a public method namedLongRunningCode
may be of one crore lines of code.... And on receiving broadcast in receiver , I will make a object of that class by passingcontext
of receiver and will simply callLongRunningCode
method with thatobject
Hope it helps