0

I've created an application where you press "start logging" which starts a service that periodically logs data. The button changes to show "stop logging". You then minimize the application and allow it to log in the background whilst you go about your business. However, I have had to implement "startForeground" as the application would close/be destroyed, and with "startForeground" a notification is required.

Now, when I run the application and minimize, then re-open the app, it is fine, but if i attempt to reopen via the notification it resets the screen so that the button now says "start logging" again and when clicked, starts logging again (takes over the current service). The service was still logging even when it said "start logging" so it has something to do with the screen not saving the button state I assume?

I have tried this, but to no avail:

@Override
public void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.putString("message", (String) but1.getText());
}


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logorcheck);
    System.out.println("goes in oncreate");
    if (savedInstanceState != null) {
        but1.setText(savedInstanceState.getString("message"));

Here are a few snippets:

LoggingService.java

NotificationManager nm = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Resources res = this.getResources();
    Notification.Builder builder = new Notification.Builder(this);

    builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.arrow_up_float)
                .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.arrow_down_float))
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("App")
                .setContentText("Service running OK");
    Notification n = builder.getNotification();
    // !
    startForeground(100, n);
    return Service.START_STICKY;

LogOrCheck.java

if (isClicked) {
                Context context = getApplicationContext();
                Toast toast = Toast.makeText(context, "Please press 'MINIMIZE' at the top to continue logging.", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP|Gravity.RIGHT, 10, 55);
                toast.show();
                System.out.println("Start logging");
                but1.setText("Stop Logging");
                but1.setTextColor(Color.RED);
                // START SERVICE, DONE. (STOP IS BELOW).
                startService(myIntent);
                isClicked = false;
            } 
            else if (!isClicked) {
                System.out.println("Stop Logging");
                but1.setText("Start Logging");
                // STOP SERVICE, DONE.
                stopService(myIntent);
                but1.setTextColor(Color.BLACK);
                isClicked = true;
            }

Thanks for any help, it seems an easy fix that I can't figure out how to do it, especially with notificationBuilder. Sorry for the long message! Thanks.

fypfyp
  • 206
  • 4
  • 10

1 Answers1

0

The Simplest thing you could do would be to create a static boolean variable in your LoggingService.java something like isLogging and set it accordingly in the service when changes occur.

In the onResume() of the activity access that variable and set the button state accordingly.

Akash
  • 631
  • 1
  • 8
  • 16