Search code examples
androidbroadcastreceiverandroid-serviceandroid-location

How to stop service from logout button?


Hello I am building an app where I am using service for locations. That service have to work all the time, so I set that when service goes to onDestroy, then intent is sent to BroadcastReceiver who starts service again, and on that way, my service always works. But I want to stop service when user click on button logout. How to prevent that,when service goes to onDestroy, don't send intent to broadcast(but only when user clicks on logout)? This is my logout from MainActivity:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_log_out) {
            if (isNetworkConnected()) {
                unsubscribingFromFirebase();
                removeTokenAndAccountFromSharedPreferences();

                stopService(mServiceIntent);
                Log.i("MAIN_ACTIVITY", "Logout!");

                Log.d("MAIN_ACTIVITY " , "Internet access ");
            }
            else {
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.need_internet), Toast.LENGTH_SHORT).show();
            }
        }

These are methods onStartCommand, onCreate and onDestroy in location service:

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("onStartCommand" , "LocationService");
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        isEnded = false;
        mRequestingLocationUpdates = false;
        mLastUpdateTime = "";
        Bugsnag.init(this, BUGSNAG_API_KEY);

        buildGoogleApiClient();
        Log.d("onCreateService", "onCreateService");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("EXIT", "onDestroy!");
        timeWhenServiceDestroyed = DateFormat.getTimeInstance().format(new Date());
        SharedPreferences sharedPreferences = getSharedPreferences(getResources().getString(R.string.user_location), MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("onDestroyService", timeWhenServiceDestroyed);
        editor.apply();
        stopLocationUpdates();
        Log.d("onDestroyService", "onDestroy: + " + timeWhenServiceDestroyed);
        Intent broadcastIntent = new Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
        sendBroadcast(broadcastIntent);
    }

And this is my BroadCastReceiver:

@Override
    public void onReceive(Context context, Intent intent) {
        Log.i(LocationServiceRestartBroadcastReceiver.class.getSimpleName(), "Service Stops! Oooooooooooooppppssssss!!!!");
        context.startService(new Intent(context, LocationService.class));
    }

How to prevent that when user clicks on logout button, my service don't create again? Thanks in advance.


Solution

  • You could use static boolean variable shouldRestart that is false only when logout button is clicked, otherwise true.

    Add to your activity :

    public static boolean shouldRestart=true;

    Add to your "logout button click event" this line:

    shouldRestart=false;

    And add this lines in onDestroy method of service :

    if (MainActivity.shouldRestart){
    Intent broadcastIntent = new Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
        sendBroadcast(broadcastIntent);
    }
    MainActivity.shouldRestart=true;