Bohrend
Bohrend

Reputation: 1497

StopService from another Activity not working?

I know there are a lot of questions on this already but it doesn't seem to work, so basically I have a custom Service, the service works fine when the timer inside it expires, and calls the stopservice method. however when I try and stop the service from another activity it doesn't work?

code for custom service:

public class ConnectionService extends Service {

Intent service_intent;

Timer t;
int expiry_time = 0;

int timer_tick_starter = 0;

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

@Override
public void onCreate() {
    t = new Timer();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    try {

        service_intent = intent;

        Log.d("onConnectionService", "onStartCommand");

        expiry_time = Integer.parseInt(intent.getStringExtra("expiry"));

        //convert the minutes to milliseconds
        timer_tick_starter = (expiry_time * 60000);

        Log.d("onConnectionService", "Expiry Time (in milliseconds) " + timer_tick_starter);


    } catch (Exception e) {

        Log.d("onConnectionService", e.getMessage().toString());

    }

    //timer will only start to run on the expiry_time which will then close the connection
    t.schedule(new TimerTask() {
        @Override
        public void run() {

            Log.d("onConnectionService", "inside expiry time");
            //close the connection

            stopService(service_intent);

                       }
    }, 30000);


    return START_NOT_STICKY;
}

@Override
public boolean stopService(Intent name) {

    Log.d("onConnectionService", "StopService called");
    String message = name.getStringExtra("msg");
    //broadcast sender
    Intent intent = new Intent();
    intent.putExtra("msg", message);
    intent.setAction("fi.android.inetkeyapplication.CUSTOM_INTENT");
    intent.putExtra("is_selected", UserConnectionState.getInstance().is_selected);

    sendBroadcast(intent);

    Log.d("onConnectionService", "Service has been killed");

    return super.stopService(name);
}

@Override
public void onDestroy() {

    Log.d("onConnectionService", "Destroy called");

}

}

here I start the service from onCreate:

  Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {

            Log.d("onConfigurationActivity", "Service starting ");
            // Intent i = new Intent(ConfigurationActivity.this, ConnectionService.class);
            Intent i = new Intent(context, ConnectionService.class);

            i.putExtra("expiry", "1");

            startService(i);

        }
    });
    thread.start();

however later in my app when I try to kill it from the activity in which I start it, it doesn't work:

  Button exit_button = (Button) findViewById(R.id.btnExit);
    connect_button.setTypeface(roboto_light);

    exit_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //do closing call
            response_close = "";


            //need to close service
            //also kill the service
            Log.d("onConfigurationActivity", "killing service");
            Intent intent = new Intent(context, ConnectionService.class);
            intent.putExtra("msg", "You have been disconnected");
            stopService(intent);
    }
    });

I have tried my class - ConfigurationActivity.this (as context) and I have tried (this) at the moment I am using a custom context that I simply get inside onCreate. however nothing seems to work what am I missing?

thanx

Upvotes: 3

Views: 1581

Answers (1)

Bohrend
Bohrend

Reputation: 1497

ok so apparently stopping the service doesn't stop the timer. so I just called cancel() on timer

Upvotes: 1

Related Questions