JobIntentService - onHandleWork is not always called? - android

I have home screen widget, which has a simple AppWidgetProvider and JobIntentService, where I do all the work.
Problem is - it works kind of randomly. Sometimes it does, sometimes it doesnt - weirdest thing is, I can see in the log, that on each widget update enqueueWork method of JobIntentService is ALWAYS called, but onHandleWork method only sometimes.
(I have found there is strong, though not 100% correlancy with battery optimization. If I turn of "Manage apps automatically", then it 99% works reliably. If it is turned on, its like flipping a coin. Sometimes it does, sometimes it doesnt. Best to ilustrate behavior would be this short simple video
This is my code (Widget provider):
public class MyWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.v("aaa", "onUpdate");
// super.onUpdate(context, appWidgetManager, appWidgetIds);
// update in my own Service (to avoid ANR)
Intent intent = new Intent(context, MyJobIntentService.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
MyJobIntentService.enqueueWork(context, intent);
}
#Override
public void onReceive(Context context, Intent intent) {
Log.v("aaa", "onReceive");
super.onReceive(context, intent);
}
}
And this is my service (JobIntentService) where I do all work:
public class MyJobIntentService extends JobIntentService {
public static final int JOB_ID = 1;
public static void enqueueWork(Context context, Intent work) {
Log.v("aaa", "enqueueWork: ");
enqueueWork(context, MyJobIntentService .class, JOB_ID, work);
}
#Override
protected void onHandleWork(Intent intent) {
Log.v("aaa", "onHandleWork: ");
}
}

This is the same thing that is happening to me. What I've found out is that in the android documentation, it says:
When running as a pre-O service, the act of enqueueing work will
generally start the service immediately, regardless of whether the
device is dozing or in other conditions. When running as a Job, it
will be subject to standard JobScheduler policies for a Job with a
JobInfo.Builder.setOverrideDeadline(long) of 0: the job will not run
while the device is dozing, it may get delayed more than a service if
the device is under strong memory pressure with lots of demand to run
jobs.
This means in Android O and above devices, it is not guaranteed that the jobIntentService starts immediately, but start with the delay depending on the device's condition. But, some phone manufacturers are deliberately canceling the job for better battery life (Like OnePlus). So the job is never called.
The workaround that I used is to create a LOW_IMPORTANCE foreground service and call it using:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(startIntent);
} else {
context.startService(startIntent);
}
But the drawback is that the notification is shown for a small duration till the work is complete.
Maybe google should put some restrictions on the device manufacturers on how to manage the background apps.

Related

Continually Running Background Service

I'm targeting sdk version 27 with a minimum version of 19 and trying to get a service that runs continuously in the background. I tried different service start options but it still got killed with the app. I tried using a BroadcastReceiver to start the service when it got killed but that gave me an error saying that the app was in the background and couldn't start a service so I tried using the JobScheduler and that gave me the same error. How is this supposed to be done? For example, if I were making a pedometer app, how could I keep that running in the background?
In oreo release Android defined limits to background services.
To improve the user experience, Android 8.0 (API level 26) imposes
limitations on what apps can do while running in the background.
Still if app need to run its service always, then we can create foreground service.
Background Service Limitations: While an app is idle, there are limits
to its use of background services. This does not apply to foreground
services, which are more noticeable to the user.
So create a foreground service. In which you will put a notification for user while your service is running. See this answer (There are many others)
Now what if you don't want a notification for your service. A solution is for that.
You can create some periodic task that will start your service, service will do its work and stops itself. By this your app will not be considered battery draining.
You can create periodic task with Alarm Manager, Job Scheduler, Evernote-Jobs or Work Manager.
Instead of telling pros & cons of each one. I just tell you best. Work manager is best solution for periodic tasks. Which was introduced with Android Architecture Component.
Unlike Job-Scheduler(only >21 API) it will work for all versions.
Also it starts work after a Doze-Standby mode.
Make a Android Boot Receiver for scheduling service after device boot.
I created forever running service with Work-Manager, that is working perfectly.
Since Android 8.0 many background service limitations have been introduced.
Two solutions:
if you need to get total control of task and execution timing, you have to choose Foreground Service.
Pros: your app will be considered to be alive, then is more unlikely that the os will kill it to free resources.
Cons: your user will always see the Foreground Notification.
if you need to schedule periodically task, then Work Manager (introduced in Google I/O 18) is the best solution. This component choose the best possible scheduler (Jobscheduler, JobDispatcher, AlarmManager..). Keep in mind that work manager APIs are useful only for the tasks that require guaranteed execution and they are deferrable.
Ref: Android Dev Documentation
The only solution I would suggest is using Firebase Cloud Messages.
Or foreground services.
Using BroadcastReciever we can run backgrouund service continuously, but if it will get killed , destroy automatically re-instance the old service instance
When service stops forcefully it will call onDestroy() method, in this case use one receiver and send one broadcast when ever service destroy and restart service again. in thee following method com.android.app is custom action of reciever class which extends BroadcastReciever
public void onDestroy() {
try {
myTimer.cancel();
timerTask.cancel();
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent("com.android.app");
intent.putExtra("valueone", "tostoreagain");
sendBroadcast(intent);
}
and in onReceive Method
#Override
public void onReceive(Context context, Intent intent) {
Log.i("Service Stoped", "call service again");
context.startService(new Intent(context, ServiceCheckWork.class));
}
In case device is restarted then we have onBootCompleted action for receiver to catch
When you are targeting SdkVersion "O"
In MainActivity.java define getPendingIntent()
private PendingIntent getPendingIntent() {
Intent intent = new Intent(this, YourBroadcastReceiver.class);
intent.setAction(YourBroadcastReceiver.ACTION_PROCESS_UPDATES);
return PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
here we use PendingIntent with BroadcastReceiver and This BroadcastReceiver has already been defined in AndroidManifest.xml.
Now in YourBroadcastReceiver.java class which contains an onReceive() method:
Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_PROCESS_UPDATES.equals(action)) {
NotificationResult result = NotificationResult.extractResult(intent);
if (result != null) {
List<Notification> notifications = result.getNotification();
NotificationResultHelper notificationResultHelper = new
NotificationResultHelper(
context, notifications);
// Save the notification data to SharedPreferences.
notificationResultHelper.saveResults();
// Show notification with the notification data.
notificationResultHelper.showNotification();
Log.i(TAG,
NotificationResultHelper.getSavedNotificationResult(context));
}
}
}
}
as you say:
I tried using a BroadcastReceiver to start the service when it got
killed but that gave me an error saying that the app was in the
background and couldn't start a service
in Oreo when you are in background and you want to start a service that service must be a foreground service use this code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
if you use this code in Oreo you have a few seconds in onStartCommand to start foreground otherwise your service considered as not responding and may be force close by user (in Android 8 or above)
There is no need to use BroadcastReceiver to start service after it is closed it is enough to just return START_STICKY or START_REDELIVER_INTENT from onStartCommand of your service to restart service after it is closed
A working hack for this is to simply start a foreground service which is only visible for the fraction of a second and starts your background service. In the background service you'd then periodically start the foreground service.
Before I give an example you should really ask yourself if this is the way to go for you, there might be other solutions to given problems (like using JobIntentService etc.); and keep in mind that this is a hack, it might be patched some time around and I'd generally not use it (I tested it with screen off and battery saving enabled though and it stayed alive the whole time - but this might prevent your device from dozing.. again, this is a dirty hack!)
Example:
public class TemporaryForegroundService extends Service {
public static final int NOTIFICATION_ID = 666;
private static Notification notification;
#Override
public void onCreate() {
super.onCreate();
if(notification == null)
notification = new NotificationCompat.Builder(this, NotificationChannels.importantChannel(this)).
setSmallIcon(R.mipmap.ic_launcher).setContentTitle("The unseen blade").setContentText("If you see me, congrats to you.").build();
startForeground(NOTIFICATION_ID, notification);
startService(new Intent(this, PermanentBackgroundService.class));
stopForeground(true);
stopSelf();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
public class PermanentBackgroundService extends Service {
private Runnable keepAliveRunnable = new Runnable() {
#Override
public void run() {
keepServiceAlive();
if(handler != null) handler.postDelayed(this, 15*1000);
}
};
private Handler handler;
public void onCreate(){
handler = new Handler();
handler.postDelayed(keepAliveRunnable, 30* 1000);
}
public void onDestroy() {
super.onDestroy();
keepServiceAlive();
}
private void keepServiceAlive() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(PermanentBackgroundService.this, TemporaryForegroundService .class));
} else {
startService(new Intent(PermanentBackgroundService.this, TemporaryForegroundService .class));
}
}
}

Foreground Service killed on Huawei (GRA-UL00) - Protected Apps Enabled

My foreground sticky service is killed after a few hours without being restarted. I know this has been asked a couple of times, and I have read and verified all the checks on my device. Its important to note that this seems to occur only on Huawei devices.
So allow me to provide the following details.
Periodic Service
public class PeriodicService extends Service {
#Override
public void onCreate() {
super.onCreate();
acquireWakeLock();
foregroundify();
}
private void foregroundify() {
// Omitted for brevity. Yes it does starts a foreground service with a notification
// verified with adb shell dumpsys activity processes > tmp.txt
// entry in tmp.txt => "Proc # 1: prcp T/S/SF trm: 0 14790:my.app.package.indentifier/u0a172 (fg-service)"
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
acquireWakeLock();
if (!isServiceRunningInForeground(this, this.getClass())){
foregroundify();
}
PeriodicAlarmManager alarmManager = PeriodicAlarmManager.get(this);
alarmManager.setAlarm();
return START_STICKY; // after a few hours, service terminates after this returns. verified in my local logs
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
releaseWakeLock();
stopForeground(true);
super.onDestroy();
}
}
PeriodicAlarmManager
public void setAlarm() {
Intent intent = new Intent(mContext, PeriodicAlarmReceiver.class);
intent.setAction("repeat");
mAlarmIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
mAlarmManager.cancel(mAlarmIntent);
long triggerAtMillis = System.currentTimeMillis() + ALARM_INTERVAL_MINUTES;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, mAlarmIntent);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, mAlarmIntent);
}
ComponentName receiver = new ComponentName(mContext, PeriodicBootReceiver.class);
PackageManager pm = mContext.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
PeriodicAlarmReceiver
public class PeriodicAlarmReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, PeriodicService.class);
service.putExtra("source", "PeriodicAlarmReceiver");
intent.getAction()));
startWakefulService(context, service);
}
}
Application
public class MyApp extends Application {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onLowMemory(){
super.onLowMemory(); // never gets called
}
#Override
public void onTrimMemory(int level){
super.onTrimMemory(level); // only gets called on app launch
}
#Override
public void onTerminate() {
super.onTerminate();
}
}
adb shell dumpsys activity processes > tmp.txt
Entry in tmp.txt => "Proc # 1: prcp T/S/SF trm: 0 14790:my.app.package.indentifier/u0a172 (fg-service)"
Above Entry is based on accepted answer here: Foreground service being killed by Android
Added MyApp to protected app list in Settings-> Advanced Settings -> Battery Manager -> Protected Apps (Allow app to keep running after screen is turned off)
Used Performance (lowest setting) in Settings-> Advanced Settings -> Power Plan (Performance)
Device Information
Model Number: HUAWEI GRA-UL00
EMUI Version: EMUI 4.0.1
Android Version: 6.0
Other Notes:
Low Memory, onTrimMemory is not called prior to termination. In any case, I stripped the app to its bare minimum just to keep the app alive in the background, so memory should not be an issue here.
Sticky Service is never restarted unless user explicitly re-launches the app.
Alarm Manager is not called to restart/recreate service. setExactAndAllowWhileIdle() does not work either, and should be irrelevant since the service is a foreground priority service, and hence should not be affected by doze mode.
Service can only run at the maximum of 12 hours before being terminated. Battery was above 65% when this happened.
It is a requirement to keep the service running indefinitely as this app is for a research project.
Is there anything else I can do or is this a specific Huawei Android modification that the developer can do nothing about. To reiterate, this issue only happens on Huawei devices.
Appreciate any additional insight on this!
Are you absolutely sure you need the wakelock? I have a similar service and I have noticed that it works even without the wakelock. This post claims that the killer is the wakelock.
I have tried with my process which used to be killed in minutes and it has now been running for hours.
Huawei -> have a battery settings, but it's not about power save mode. under this battery settings screen, there is sub-menu call "Protected App" (not sure the name). you need to allow your app to be protected to prevent Huawei kill app after lock the screen.
It sounds like your app is being killed by Huawei PowerGenie because it holds a wake lock indefinitely. If you can't avoid using a wake lock, please see my answer to a similar question for a workaround.

IntentService is killed when app is stopped

I've this IntentService:
public class ServiceUpdateNewResults extends IntentService{
private void setAlarmToCheckUpdates() {
Calendar calendar = Calendar.getInstance();
//calendar.add(Calendar.DAY_OF_YEAR, 1); //dema
//calendar.set(Calendar.HOUR_OF_DAY, 22); //a les 10
calendar.add(Calendar.SECOND, 20);
Intent myIntent = new Intent(this.getApplicationContext(), ReceiverCheckUpdates.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)this.getApplicationContext().getSystemService(this.getApplicationContext().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
public ServiceUpdateNewResults() {
super("ServiceUpdateNewResults");
}
#Override
protected void onHandleIntent(Intent intent) {
//fem les coses.
//Toast.makeText(this.getApplicationContext(), "holaa", Toast.LENGTH_LONG).show();
setAlarmToCheckUpdates();
Log.d("debugging","hello");
}
}
And this is calling a BroadCastReceiver every 20 seconds, which ends up calling this service, and this is going to happen "forever". (in a future it will be 1 day, not 20 seconds).
This is the Receiver:
public class ReceiverCheckUpdates extends BroadcastReceiver{
Context context;
#Override
public void onReceive(Context context, Intent intent){
this.context = context;
Intent service1 = new Intent(context, ServiceUpdateNewResults.class);
context.startService(service1);
}
}
This is working perfectly, but if I stop the app from Android settings, the service is also stopped. I want to avoid this. I want that if the App is closed, the service should keep working.
Is it possible?
Actually, when is a service killed ?
if I stop the app from Android settings, the service is also stopped
If by "stop the app from Android settings", you mean that you press the Force Stop button, your app will not run again until something manually runs one of your components (e.g., user launches your activity). More specifically in this case, your alarms are unscheduled.
I want to avoid this.
Then do not press the "Force Stop" button.
I want that if the App is closed, the service should keep working.
In any non-"Force Stop" scenario, the alarms will keep firing (at least until the device falls asleep, given your current implementation).
The IntentService is part of your app. If the system destroys your app, it will destroy the IntentService. You can reduce the chances of this happening by putting the IntentService in a separate process, but you can't stop the system from destroying an IntentService. You can make it highly unlikely that the system will destroy a Service; to do that, you use a "foreground" Service. However, you should avoid doing this unless you really really need to. In addition, you can't have a foreground IntentService, so you'll have to add your own background Handler and HandlerThread.

Broadcast Receiver in a service but intent not getting caught on service once process is killed

I have a simple widget that does some calculations once the screen comes on and displays them and clears all the fields once the screen goes off ... i have a broadcast receiver setup in my service which listens to ACTION_SCREEN_ON and ACTION_SCREEN_OFF.
This works perfectly as long as the phone doesn't go to sleep for a long period of time or there is heavy usage of the phone - once this happens my widget process is killed (the service is still running but the process is killed) after this when the screen goes off and comes back on my widget doesn't update as the ACTION_SCREEN_ON intent is not caught by my service :(
public class CDTservice extends Service {
#Override
public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
m_receiver = new ScreenBroadcastReceiver();
registerReceiver(m_receiver, filter);
Log.d("Widgettool", "works");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
start();
Toast.makeText(getBaseContext(), "SERVICE ON", Toast.LENGTH_SHORT).show();
return START_REDELIVER_INTENT;
}
#Override
public void onDestroy() {
stop();
unregisterReceiver(m_receiver);
}
public void start()
{
RemoteViews View = new RemoteViews(this.getPackageName(), R.layout.main);
updatewidgetclass x= new updatewidgetclass(this, View, widgetId);
x.start(); // does calculations and displays on widget
}
public void stop()
{
RemoteViews Viewclear = new RemoteViews(this.getPackageName(), R.layout.main);
updatewidgetclass y = new updatewidgetclass(this, Viewclear, widgetId);
y.stop(); // clears resources and stops
}
private class ScreenBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("ON SCREEN ON", "might hang here");
start();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
stop();
}
}
}
sometimes when the widget process is not claimed by the android system the widget works perfectly for days and perfectly displays the values ACTION_SCREEN_ON
the problem arises when - i check in settings>apps>running - i can see my widget name and it says 0 processes and 1 service
i assume the broadcast receive is happening on the main process and hence its not receiving it when the process gets killed.
I have a work around in place for this but would really like to fix the issue.
Any helps is highly appreciated
I agree with #CommonsWare that having a service running in the bg at all times just to detect when the screen turns on and off is a very bad idea. Do you really need ACTION_SCREEN_ON, or will ACTION_USER_PRESENT (phone unlocked) suffice? This way, you do not need a service at all, and you can just define the receiver in the manifest.
If you really wanted to, you could register your service/ACTION_SCREEN_OFFreceiver in your ACTION_USER_PRESENT receiver so that the service is only running when the user is actually using the device.
I know this doesn't really answer your question, but it does provide a useful workaround to your problem.

Disable update on battery percentage

I have a service that performs background updates.
I want to give the user the the option to disable the updates when their battery percentage reaches a certain level.
From my research, I'm going to use a receiver in the onCreate method of my Service class, eg:
public class MainService extends Service
{
#Override
public void onCreate()
{
this.registerReceiver(this.BatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver BatInfoReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent intent) {
int level = intent.getIntExtra("level", 0);
}
};
}
I'm assuming that the best practice is to leave the service running, check the battery level in the service, and not perform the CPU-intensive code, based on the percentage?
I don't actually need to stop the service itself and start it up again, based on the battery percentage?
UPDATE:
This seems to be a better solution, but not 100% sure. I registered a BroadcastReceiver in the AndroidManifest:
<receiver android:name="BatteryReceiver" />
Then created a BroadcastReceiver:
public class BatteryReceiver extends BroadcastReceiver
{
#Override
public void onReceive(final Context context, final Intent intent)
{
final int currentBatteryPercent = intent.getIntExtra("level", 0);
final int disableBatteryPercent = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString("batteryPercent", 0);
//AlarmReceiver is the service that performs the background updates
final ComponentName component = new ComponentName(context, AlarmReceiver.class);
if (currentBatteryPercent < disableBatteryPercent)
{
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
}
else
{
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);
}
}
}
That's right. What you will typically do is schedule some broadcast intent for an update (perhaps through an AlarmManager). When you get the notification that there is a low battery, you will stow this away in your service, and then before doing an update, check to ensure that the battery isn't too low. here is a good tutorial of watching the battery level. You shouldn't do much when handling this intent, just stick the battery level somewhere and (before doing an update) make sure it's appropriately "full."
Your update is a really bad way to stop an app. In general, asking the package manager to stop your component is much more of a hack than a solution. Instead, you should move some code into the component that actually does the updating, and store / update the information for the battery info there. Before you do an update, you check the battery level and don't proceed unless it's at a level where you feel comfortable updating the app. Using a broadcast receiver is fine, but you need to store the level somewhere (perhaps a sharedprefs). Instead, this is why putting the receiver within the service where you do the updating is probably best.
Is it possible to manage this requirement with a broadcastReceiver instead of running a service continuously ?
public void onReceive(final Context context, final Intent intent) {
if(intent != null && intent.getAction() != null) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
// Set alarm
}
else if(action.equals(Intent.ACTION_BATTERY_LOW)) {
setLocationAlarmReceiverEnabled(context, false);
}
else if(action.equals(Intent.ACTION_BATTERY_OKAY)) {
setLocationAlarmReceiverEnabled(context, true);
}
}
}

Categories

Resources