Serivces on Android rebooting variables

Asked

Viewed 51 times

0

Me developing a test application in which a Service is running in Background and is sending notifications to your phone every 10 seconds. To improve the test I created a counter that increases with each notification sent. The Service works almost perfectly, however, if I open and close the APP the counter restarts to 1 and I have no idea why. I talked about closing the APP and not minimizing, there is a big difference between minimize the APP and close, if I minimize remains normal but if I close it yes the counter restarts.

Service class:

public class BackgroundService extends Service {

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

    public sattic int j = 1;


   @Override
   public void onCreate() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(BackgroundService.this)
                            .setContentTitle("Notificação: " + j)
                            .setContentText("Texto da notificação: " + j)
                            .setTicker("Texto que aparece ao receber a notificação.")
                            .setBadgeIconType(R.drawable.error)
                            .setSmallIcon(R.drawable.help)
                            .setAutoCancel(true);

                    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(1, builder.build());
                     j++;
                    try {
                        Thread.sleep(10000);
                    } catch (Throwable e) {
                        Log.create(e);
                    }
                }
            }
        }.start();
   }
}

Main class:

     public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            startService(new Intent(getBaseContext(), BackgroundService.class));
        }
}

In short, if I open the app the counter and the notifications work normally, however if I close the application the counter restarts from 1 (the notifications keep appearing but the counter restarts). I talked about closing the APP and not minimizing, there is a big difference between minimize the APP and close, if I minimize remains normal but if I close it yes the counter restarts.

I already tried to put the counter variable inside the Thread but it didn’t work it restarts the same way.

  • I don’t get it. It’s not natural to reboot?

  • @So you’re telling me that it’s normal to reboot? If I had it there instead of an int a Socket would I lose the connection? Or if I had saved a user name too for example, I would lose it just because it closed the App?

1 answer

0

it is normal to restart, every time the service is activated, it will create a new instance of Service with the default values of the class, if you want to make a counter with the old value, I suggest you store the current value every time you finish running Service and access this value when you start Service

Here are some options of Data Storage on Android to do this

https://developer.android.com/guide/topics/data/data-storage?hl=pt-br

  • But what if I have nonserializable objects that can’t be stored? Will I lose this information if the guy closes the APP? I thought the idea of the Service was just to work in the background. Is there any way to secure or maintain these objects?

  • I have taken a look at the documentation and the service only instantiated once, and in that opportunity the onCreate() method is called and soon after the onStartCommand() method, when it is "instantiated" again then the onCreate() method will not only be called the onStartCommand() method. So it doesn’t make sense that, or maybe I misunderstood the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.