0

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");
    }
}
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
Jaitnium
  • 621
  • 1
  • 13
  • 27
  • what is your service doing? you should have it check the value of the preference when its in middle of doing work – SteelToe Mar 19 '19 at 03:18
  • @SteelToe The service blocks phonecalls. It does check if the local variable "enable" in main is true or false. Are you saying I need to check preferences instead of the variable? Doesn't the code above update the variable? I thought if the app instance was closed it would update the variable which would update it in the service as well. – Jaitnium Mar 19 '19 at 03:48
  • oh you want to stop the service when the user toggles the switch to the disabled state? – SteelToe Mar 19 '19 at 03:49
  • @SteelToe No I want the code that blocks calls to exit after checking the variable. – Jaitnium Mar 19 '19 at 03:54
  • meaning you want to kill the service that is checking for calls? – SteelToe Mar 19 '19 at 03:55
  • @SteelToe Meaning if the user opens the app and clicks the button, the service should stop checking for calls but should still be running. If the user reopens the app and enables the app, the service should resume blocking calls. – Jaitnium Mar 19 '19 at 03:57
  • ah, can you post your service code? – SteelToe Mar 19 '19 at 03:58
  • @SteelToe Check the edit. – Jaitnium Mar 19 '19 at 04:07
  • Where is the `Service` checking the preferences? – David Wasser Mar 19 '19 at 15:57

1 Answers1

0

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.

Jaitnium
  • 621
  • 1
  • 13
  • 27