In my app i am using a Service that periodically checks if there is a new personal message for the logged in user. The service is started if the user enables the notification feature. Now if the user disables the notification feature i would like to stop the service.
I try to stop the service with the following lines of code.
Intent service = new Intent(getApplicationContext(), MessageService.class); stopService(service); The problem is that the service doesn't stop. It goes on working.
Here you can see my message service.
public class MessageService extends Service { private int intervall; public MessageService(){ } @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent,flags,startId); Bundle intentData = intent.getExtras(); if(intentData != null) { this.intervall = intentData.getInt("intervall"); } final Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); // async task for calling api otherwise we get an exeception here new ServiceMessagesTask().execute(MessageService.this); } }; new Thread(new Runnable(){ public void run() { while(true) { try { Thread.sleep(intervall); // repeat after given intervall handler.sendEmptyMessage(0); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); } }
I have an activity where the user can edit his preferences. There it is also possible to activate the notification feature.
The notification service is started or stoped in the savePreferences() method:
public void savePreferences(View button) { EditText login = (EditText)findViewById(R.id.txtbLogin); EditText password = (EditText)findViewById(R.id.txtbPassword); CheckBox enableNotification = (CheckBox) findViewById(R.id.cbNotifications); Spinner spinner = (Spinner) findViewById(R.id.notificationInterval); if(!login.getText().equals("") && !password.getText().equals("")){ Map<String, Object> preferences = new HashMap<String, Object>(); preferences.put("document_type", CouchbaseHelper.CB_VIEW_USER_PREFERENCES); preferences.put("login", login.getText().toString()); preferences.put("password", password.getText().toString()); if(enableNotification.isChecked()){ preferences.put("enableNotification", true); } else { preferences.put("enableNotification", false); } preferences.put("notificationInterval", this.notificationInterval); CouchbaseHelper couchbaseHelper = new CouchbaseHelper(getApplicationContext()); String documentId = couchbaseHelper.createDocUserPreferences(preferences); couchbaseHelper.closeDb(); // start notification service if enabled if(enableNotification.isChecked()){ Intent service = new Intent(getApplicationContext(), MessageService.class); service.putExtra("intervall", Integer.valueOf(this.notificationInterval)*60*1000); startService(service); } else { // TODO: this is not working!!! service doesnt stop // try to stop running service if(isMyServiceRunning()){ Intent service = new Intent(getApplicationContext(), MessageService.class); stopService(service); } } } finish(); Intent main = new Intent(Preferences.this, Main.class); startActivity(main); }