Search code examples
androidandroid-intentsharedpreferencesandroid-service

How can I change the preferences of a service?


I'm making an app that I want to start on boot and run in the background. I have decided to make it a service following this tutorial:

Android - Start service on boot

However, I want the user to be able to open the app and press a button to enable/disable its functionality. I have a boolean called enabled that I'm saving with SharedPreferences onStop and onStart:

//Save preferences on stop
@Override
public void onStop() {
    super.onStop();

    SharedPreferences pref = getSharedPreferences("info", MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean("AppEnabled", enabled);
    editor.commit();
}

//Load preferences on start
@Override
public void onStart() {
    super.onStart();

    SharedPreferences pref = getSharedPreferences("info", MODE_PRIVATE);
    enabled = pref.getBoolean("AppEnabled", true);

    //Make button reflect saved preference
    Button button = (Button)findViewById(R.id.enableButton);
    if(enabled) {
        button.setText("Disable");
    }
    else {
        button.setText("Enable");
    }
}

If I open the app and click the button, the functionality is toggled as desired. But if I click the button to disable the functionality, and close the app, the service running the background still thinks it's enabled. How can I properly update the service so it gets the updated variable?

EDIT:

This is registered in the manifest and called on boot:

/*This class starts MainService on boot*/
package com.example.sayonara;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

public class StartAppServiceOnBoot extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        Intent intent = new Intent(context, MainService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
        Log.i("Autostart", "started");
    }
}

This is called by the class above to start the service:

/*Called by StartAppServiceOnBoot, starts mainActivity as a service*/
package com.example.sayonara;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MainService extends Service {
    private static final String TAG = "MyService";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public void onStart(Intent intent, int startid)
    {
        Intent intents = new Intent(getBaseContext(), MainActivity.class);
        intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intents);
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    }
}

Solution

  • It turns out since I'm extending BroadCastReceiver, the class is running in the background and is independent of my app, which is why it blocks calls even when the service off. I followed this tutorial in order to disable my receiver when the service is off and it works now.