4

I use a service and within this service I have a timer, which sends every minute a message to my TCP/IP-Server.

public void keepAlive() {
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                try {
                    Gson gson = new Gson();
                    String message = gson.toJson(new StillAlive(Mode.STILLALIVE));
                    sendMessage(message);
                    Log.d("TCP-SEND", message);
                } catch(Exception e) {
                    Log.e("TCP1", e.getMessage());
                }
            }
        }, 60000, 60000);
    }

But when my mobile phone locks. Or I press the button to turn off the display, my service stops sending these Messages.

What I am not sure about is, if the service stops, when turning the display off or if just the timer stops because of any reason I am not aware of.

Does anyone know if a Timer stops in this situation? Or does a service stop?

Would appreciate some help! :-)

progNewbie
  • 4,362
  • 9
  • 48
  • 107
  • It's the wifi/mobile data that gets powered down to save on battery. On a mobile device, it's not the amount of data that consumes battery, it's really the amount of time the antenna is powered on. If you really need to check something from a server every minute, please use Google Cloud Messaging instead. Google Cloud Messaging won't increase battery usage, but if you use your technique assuming you make it work will at least consume 20% of the total battery life (even if no changes are detected). To most people 20% is a lot, especially for just one application. Please use Google Cloud Messagi – Stephan Branczyk Jan 11 '16 at 10:46
  • @StephanBranczyk: My Problem is, that I need to send these messages to tell my TCP-Server, that the connection is still up. I could also send this just every 5 Minutes or something like that, if this would work. But I need the message to be send in a known time-frame. Is this possible? – progNewbie Jan 11 '16 at 11:03
  • @StephanBranczyk I wonder how whatsapp/telegram etc. are handling this problem. Are they using google cloud messaging? – progNewbie Jan 11 '16 at 11:58
  • The FAQ from whatsapp seems to imply that they are using GCM. See http://www.whatsapp.com/faq/en/android/20887936 "If all of the above steps do not help, it is possible that you are not receiving updates from Google's push notification service or that the settings of your Wi-Fi or Mobile data are misconfigured." – Stephan Branczyk Jan 11 '16 at 12:10
  • Telegram seems to be using both GCM and their custom solution (in case GCM doesn't work). https://telegram.org/faq "We currently have two types of notifications on Android: GCM and our own custom notification service that is independent from Google. Note that Google notifications (GCM) just won‘t work properly on some Android devices. The Telegram notification service is reliable but requires additional battery resources. It doesn’t and shouldn't take much, though, so please report all drastic battery-draining cases immediately." – Stephan Branczyk Jan 11 '16 at 12:10
  • @StephanBranczyk Just that I get it the right way: I could use GCM instead of the whole tcp/ip-connection? My App is also a messaging app like telegram/whatsapp etc. So I could replace my tcp/ip-connection completly with GCM, right? – progNewbie Jan 11 '16 at 12:59
  • @StephanBranczyk: I read that their own notificationsystem without GCM doesn't need much more battery power: https://twitter.com/telegram/status/478487472750755840 So I think there has to be a way, to send messages when screen is off without wasting battery life. – progNewbie Jan 11 '16 at 13:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100401/discussion-between-stephan-branczyk-and-prognewbie). – Stephan Branczyk Jan 11 '16 at 17:12

2 Answers2

3

This is because of your device starts to sleep. This is highly recommended to use the other implementation for such processes: AlarmManager

It looks something like this (this is a part of my code):

private static AlarmManager am;

//....
@SuppressLint("NewApi")
public static void startByAlarm(Context ctx, boolean wakeup, long nexttime, boolean autoStart)
    {
PendingIntent pi = wakeup? PendingIntent.getBroadcast(ctx, _.intentalarm, intent, PendingIntent.FLAG_CANCEL_CURRENT):
            PendingIntent.getService(ctx, _.intentalarm, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am = (AlarmManager) ctx.getSystemService(Activity.ALARM_SERVICE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.KITKAT){
        am.setExact(wakeup?AlarmManager.RTC_WAKEUP:AlarmManager.RTC, nexttime, pi);
    } else{
        am.set(wakeup?AlarmManager.RTC_WAKEUP:AlarmManager.RTC, nexttime, pi);
    }
//or am.setRepeating ...
}
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • Thanks for the quick answer: So this means, that it is a Timer problem? :-) – progNewbie Jan 11 '16 at 10:41
  • @progNewbie, I found that a lot of phone has got different behavoiur for long-term processes. yes, you can say, this is a timer problem. But the core problem is 'battery life consuming'. If you want to ping your server with 100% probability you have to you alarm manager. Futher more, I don't know whether your code use timer.stop() is some parts of your code:) – Vyacheslav Jan 11 '16 at 10:43
0

Have a look at This answer by jscharf which may be helpful to you but you have to be alert for this because it's a battery consumptioning task.

Community
  • 1
  • 1
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68