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();
}
}