0

I'm calling a service every three minutes using the following function:

   public void RunBackgroundService() {
        final Handler service_handler = new Handler();

        Timer timer = new Timer();
        TimerTask doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                service_handler.post(new Runnable() {
                    public void run() {
                        context.startService(new Intent(context,BackgroundService.class));
                    }
                });
            }
        };
        timer.schedule(doAsynchronousTask, 0, 180 * 1000);
    }

And this is my service in which I'm launching a notification:

public class BackgroundService extends Service {


    Helper helper;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        //Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        //Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onStart(Intent intent, int startid)
    {
        helper=new Helper(this);
       // Log.d("Service started at:", helper.getdatetime());

        Intent n_intent = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, n_intent, 0);

// build notification
// the addAction re-use the same intent to keep the example short
        long[] vibrate = { 0, 100, 200, 300 };
        Notification n  = new Notification.Builder(this)
                .setContentTitle("Application")
                .setContentText("This is a test")
                .setSmallIcon(R.drawable.notification_icon)
                .setContentIntent(pIntent)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setVibrate(vibrate)
                .setAutoCancel(true)
                .build();


        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, n);
    }
}

It's very important to me to remain the service running at all time, when the app crashes the service stops. How can I prevent the service from stopping ?

Monzer Yaghi
  • 1,300
  • 1
  • 10
  • 21
  • What's `Helper`? Other than that the service just posts the notification and does nothing until it eventually dies of boredom. And this happens every three minutes. If you need to run a task periodically, look into https://github.com/evernote/android-job. – Eugen Pechanec Apr 16 '16 at 20:39

3 Answers3

0

When an app crashes, the entire process goes down with it. This includes all Services and other threads in that process. You can't stop that from happening.

The best you can do is to configure your app to run in a different process from the main app process, but then you have to know how that affects the rest of your app.

Be sure to read the docs for how to define a service to run in another process. You use the android:process attribute for that.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

You cna not stop system killing your service. But what you can do is to restart the service when it is crashed. Take a look at detailed sof post How to restart service after the app is killed from recent. This solution works not only when app is killed through recent taks by user but also when system kills app.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52
0

Use android:process tag to start process in different process.

Then use it as bind to service from your application, this way you can communicate with service via ipc and service runs indefinitely even if your app crashes.

Devgeek
  • 13
  • 4