Question

Currently I am creating a program which requires a service which should always run. It works perfect but after a few hours the service is not executing any more. In the service there is a timer which will collect all the available WiFi networks and send it to a server.

After the service is not working any more and I navigate to apps that are being executed in Android I see my app with '0 process and 1 service' (Facebook, for example, says '1 process and 1 service'). That means the process is being killed so the service is not running anymore. My question to you is how to prevent this or how an I solve this? My activity is binded to the server and on the 'onStartCommand' I return 'START_STICKY'.

I also read something about AlarmManager but if I understand it well this will start the service every x seconds. What I want is a service that not stops with running until I say it or that's being restarted after the process is being killed by the Android system.

Edit I don't want to make it a foreground service, a way to restart my service when it's being stopped is fine for me. A foreground service means a notification bar, that's not what I want.

Edit 2

I am now doing it with a alarmmanager that checks if it's running or not:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    Intent broadcast_intent = new Intent(this, RunChecker.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,  broadcast_intent, 0);

    Calendar cal = Calendar.getInstance();
    if (cal.get(Calendar.SECOND) >= 1)
        cal.add(Calendar.MINUTE, 1);
    cal.set(Calendar.SECOND, 1);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 300*1000, pendingIntent);

And my receiver:

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

     ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
         List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
     boolean isFind = false;
        for (RunningServiceInfo runningServiceInfo : services) {
            //Log.v("StrollSwap", runningServiceInfo.service.getClassName());
            if (runningServiceInfo.service.getClassName().equals("com.example.strollswap.StrollSwapService")){
                isFind = true;
            }
        }
        if(!isFind)
        {
            Intent startServiceIntent = new Intent(context, StrollSwapService.class);
            context.startService(startServiceIntent);
        }

}

I know the code looks bad but it's for testing and I will rewrite it if it's working the way I want :). If there are other solutions or solutions that will definitely works please share.

Edit number 3

It seems like edit 2 is not working..no solution found yet.

Was it helpful?

Solution

To keep your service ongoing forever you need to make it a "Foreground" service. Follow this link , Running a Service in the Foreground I hope this will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top