Red-Folder / Cordova-Plugin-BackgroundService

BackroundService plugin for use with Cordova (PhoneGap)
144 stars 85 forks source link

BackgroundService stops in android 4.4 #30

Closed acuatoria closed 9 years ago

acuatoria commented 10 years ago

I'm facing this issue , that seems when android kill the app also kill the service. and I have read this http://stackoverflow.com/questions/20677781/in-android-4-4-swiping-app-out-of-recent-tasks-permanently-kills-application-wi

Red-Folder commented 10 years ago

Also found this: https://code.google.com/p/android/issues/detail?id=63793

I wonder if this related to the changes made in the operating system for low memory: http://source.android.com/devices/low-ram.html

However, I would prefer to do some basic checks before jumping to conclusions.

Some questions:

1) How is the app being killed? 2) If you run the same application on 4.3 or lower, do you get the same problem or not? 3) Can you post an example logcat of when the service is being stopped.

acuatoria commented 10 years ago

I'm running in a nexus 5, so we can discard low ram. The service is killed long ago the app it's removed from the task list, hours passed. In lower android versions it doesn't happen. And when I've got the logcat with the error I'll paste it.

teusink commented 10 years ago

True. I have tested it too on my Nexus 4. Everything is killed. I expect Android 4.4.3 in the coming weeks :).

2013/12/23 acuatoria notifications@github.com

I'm running in a nexus 5, so we can discard low ram. The service is killed long ago the app it's removed from the task list, hours passed. In lower android versions it doesn't happen. And when I've got the logcat with the error I'll paste it.

— Reply to this email directly or view it on GitHubhttps://github.com/Red-Folder/Cordova-Plugin-BackgroundService/issues/30#issuecomment-31116979 .

www.teusink.org

Red-Folder commented 10 years ago

I'd appreciate idiot proof instructions on how to replicate on a virtual device. My S3 has only just updated to 4.3 so I'm going to struggle on a real device.

acuatoria commented 10 years ago

I've got a backgroundservice running and doing some work every 15 seconds. In android 4.4.2 the service stops maybe hours since it works well, don't know yet the reason, I'm on it. When I restart the device the service starts as well. There are many issues open similar and equal in android 4.4, and seems to have workarounds, that I have proved without success. https://code.google.com/p/android/issues/detail?id=53313 https://groups.google.com/forum/#!msg/android-developers/H-DSQ4-tiac/j6SYvrwJsAIJ In lower android versions, android 4.2, it works perfect.

acuatoria commented 10 years ago

I've got the log. And the error is the proper system that kills services, take a look. Could give the source to do some workarounds in the java classes? Such as insert the START_STICKY Tag What do you think about release the apk with this bug ? so that it only affects to 4.4 Now you know why android 4.4 saves you battery :P

12-24 09:49:28.528 2618-2667/? W/DX-PowerManager﹕ [PowerStatsSnapMgr] Failed to get stats snap. Cannot add new snap. 12-24 09:49:29.388 762-7845/? I/ActivityManager﹕ Killing 3918:de.android.telnet/u0a82 (adj 9): kill background 12-24 09:49:29.398 762-1041/? I/ActivityManager﹕ Killing 4907:com.facebook.katana:providers/u0a101 (adj 15): kill background 12-24 09:49:29.408 762-1041/? I/ActivityManager﹕ Killing 3994:com.facebook.katana/u0a101 (adj 9): kill background 12-24 09:49:29.448 762-7846/? I/ActivityManager﹕ Killing 5277:com.twitter.android/u0a59 (adj 15): kill background 12-24 09:49:29.458 762-1056/? I/ActivityManager﹕ Killing 5258:la.droid.qr/u0a88 (adj 15): kill background 12-24 09:49:29.458 762-1057/? I/ActivityManager﹕ Killing 4717:com.ubicuo.contact/u0a110 (adj 15): kill background

Red-Folder commented 10 years ago

The source for the .jar file is here -> https://github.com/Red-Folder/Cordova-Plugin-BackgroundService-Source

Do the following:

The aidl files are special android interface language that allows cross process communication (in this case app to service). Eclipse will generate class files from them - thus they need to be set as source.

Good hunting ;)

teusink commented 10 years ago

I would seriously consider the wait for a little while. I expect this to be a bug that Google will fix in the next release. I cannot inmagen that tech-companies like Facebook and Twitter are already on the phone with Google about this. This is a serious bug that affects user experiences of every app with a service.

2013/12/24 Mark Taylor notifications@github.com

The source for the .jar file is here -> https://github.com/Red-Folder/Cordova-Plugin-BackgroundService-Source

Do the following:

  • Remove BackgroundService.jar from your project
  • Copy the src folder into your project.
    • In the src file, rename BackgroundServicePlugin.v3.1.0.java to BackgroundServicePlugin.java
  • Remove all the other BackgroundServicePlugin.vx.x.x.java for the src folder
  • Copy the aidl folder into your project. You'll need to set aidl as source folder (I think you can do this off a right click - if not google search will do it).
  • Do a project clean & build and you should be

The aidl files are special android interface language that allows cross process communication (in this case app to service). Eclipse will generate class files from them - thus they need to be set as source.

Good hunting ;)

— Reply to this email directly or view it on GitHubhttps://github.com/Red-Folder/Cordova-Plugin-BackgroundService/issues/30#issuecomment-31165500 .

www.teusink.org

acuatoria commented 10 years ago

Doing some test with du battery, that kills every services it can, and android 4.4.2 in nexus 5. START_STICKY tag doesn`t work, I guess by the open issue with android Facebook is killed too but restarted before, my service not, maybe using an Intent it's the solution. The only solution I've found that seems to works it's bring the service to foreground by a permanent notification in status bar. Waiting your feedback.

acuatoria commented 10 years ago

What do you think about launch an intent at the start of the Service that checks the service periodically and launch the service if it's killed?

teusink commented 10 years ago

Is this a bug in Android or by design? If it is by design I would add start_sticky (I would want to see that added anyways :) and leave it with that.

If it is a bug I would wait for Google to fix it in the next release (4.4.3 or so) IMO.

www.teusink.org Op 26 dec. 2013 15:09 schreef "acuatoria" notifications@github.com:

What do you think about launch an intent at the start of the Service that checks the service periodically and launch the service if it's killed?

— Reply to this email directly or view it on GitHubhttps://github.com/Red-Folder/Cordova-Plugin-BackgroundService/issues/30#issuecomment-31221966 .

acuatoria commented 10 years ago

I think I've got the workaround working. Still probing, but at the moment it's task-killer proof. So I think It's great. In the source of BackgroundService.java put this. onStart function leave empty of working code. I hope this will help you.

@Override public void onStart(Intent intent, int startId) { Log.i(TAG, "Service started");

    // Duplicating the call to initialiseService across onCreate and onStart
    // Done this to ensure that my initialisation code is called.
    // Found that the onStart was not called if Android was re-starting the service if killed

}

@Override public int onStartCommand(Intent intent, int flags, int startId) {

    try{

        Log.e("FLAGX : ", ServiceInfo.FLAG_STOP_WITH_TASK + "");
        Intent restartServiceIntent = new Intent(getApplicationContext(),
                this.getClass());
        restartServiceIntent.setPackage(getPackageName());

        PendingIntent restartServicePendingIntent = PendingIntent.getService(
                getApplicationContext(), 1, restartServiceIntent,
                PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager) getApplicationContext()
                .getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 5000,
                restartServicePendingIntent);

        super.onTaskRemoved(intent);
    }catch(Exception e){;}

    initialiseService();
    return START_STICKY;

}
teusink commented 10 years ago

Nice work! If the plugin js adjusted I can test it out. Op 29 dec. 2013 18:28 schreef "acuatoria" notifications@github.com:

I think I've got the workaround working. Still probing, but at the moment it's task-killer proof. So I think It's great. In the source of BackgroundService.java put this. onStart function leave empty of working code. I hope this will help you.

@Override https://github.com/Override public void onStart(Intent intent, int startId) { Log.i(TAG, "Service started");

// Duplicating the call to initialiseService across onCreate and onStart
// Done this to ensure that my initialisation code is called.
// Found that the onStart was not called if Android was re-starting the service if killed

}

@Override https://github.com/Override public int onStartCommand(Intent intent, int flags, int startId) {

try{

    Log.e("FLAGX : ", ServiceInfo.FLAG_STOP_WITH_TASK + "");
    Intent restartServiceIntent = new Intent(getApplicationContext(),
            this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(
            getApplicationContext(), 1, restartServiceIntent,
            PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext()
            .getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 5000,
            restartServicePendingIntent);

    super.onTaskRemoved(intent);
}catch(Exception e){;}

initialiseService();
return START_STICKY;

}

— Reply to this email directly or view it on GitHubhttps://github.com/Red-Folder/Cordova-Plugin-BackgroundService/issues/30#issuecomment-31321076 .

Red-Folder commented 10 years ago

Good work. It will be a good few weeks before I get a chance to merge it into the code.

This is likely to give us time to see if Google are going to treat as a bug or by design. If by design, we probably need to be comfortable that this isn't just working round design (thus susceptible to being fragile).

acuatoria commented 10 years ago

Today I've seen the records of the service and it was stopped 8 hours ago. So I think that the system has stopped the service, so the workaround doesn't fix the stop by the system.

Red-Folder commented 9 years ago

Moved to new repo -> https://github.com/Red-Folder/bgs-core/issues/29