0

I try to stop get my service state and stop it if is running. Even i use How to check if a service is running on Android?

But after stop my the service i remak that it alway run. Here is my code:

public class Sauvegarde_Mail extends Activity{

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sauvauto_layout_boitemail);
    final Button byActiver = (Button)findViewById(R.id.btactiver);
    Button byDesactiver = (Button)findViewById(R.id.btdesactiver);

    byActiver.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
                BoitedialogMailEdit();
            }
    });

    byDesactiver.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
                if(isMyServiceRunning()==true){
                stopService(new Intent(getApplicationContext(), MyServiceSauvAutoMail.class));}
                byActiver.setEnabled(true);
                Toast.makeText(getApplicationContext(),"Processus arreté",Toast.LENGTH_LONG).show();

            }               
    });
}

private Boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (MyServiceSauvAutoMail.class.getName().equals(service.service.getClassName())) {
            Log.e("i","Bjr");
            return true;
        }
    }
    return false;
}

public void BoitedialogMailEdit(){
    AlertDialog.Builder Builder = new AlertDialog.Builder(this);
     LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
     View layout = inflater.inflate(R.layout.dialog_layout3, (ViewGroup) findViewById(R.id.layout_root));
     final EditText adrmail = (EditText) layout.findViewById(R.id.adrmail);
     Builder.setView(layout);

    Builder.setPositiveButton("VALIDER",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            mail = adrmail.getText().toString();
            final Button byActiver = (Button)findViewById(R.id.btactiver);
            Intent intent2 = new Intent(getApplicationContext(), MyServiceSauvAutoMail.class);
            startService(intent2);
            dialog.dismiss();
            Toast.makeText(getApplicationContext(),"Processus démarré.",Toast.LENGTH_LONG).show();
            byActiver.setEnabled(false);
            }
        });
    AlertDialog dialog = Builder.create();
    dialog.show();
}
}
Community
  • 1
  • 1
user3274646
  • 1,901
  • 2
  • 13
  • 7

1 Answers1

0

You don't need to check whether your Service is running or not, simply call stopService() on it. It will return true if there was such a Service running, false otherwise. From the reference:

Returns: If there is a service matching the given Intent that is already running, then it is stopped and true is returned; else false is returned.

Analogously, startService() won't start several Services if called several times.

Returns: If the service is being started or is already running, the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.

nKn
  • 13,691
  • 9
  • 45
  • 62
  • In my way phicially the service stop in my device service manager. But i remark that it still run. Because the service send mail automatically – user3274646 Feb 20 '14 at 12:11
  • Then something is not working as expected. Check the result values (`boolean`s) and see whether it's the expected value or not. – nKn Feb 20 '14 at 12:23