5

I'm creating an app that has some threads and I want to close it.I tried:

System.exit(0);

But the application restart itself.

Please help me,thanks.

EDIT:

OnDestroy code:

@Override
public void onDestroy(){
    android.os.Process.killProcess(android.os.Process.myPid());
}

OnOptionsItemClicked:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.item1:
        startActivity(new Intent(this, ConnActivity.class));
        finish();
        break;
    case R.id.item2:
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
        break;
    case R.id.item3:
        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Mensaje al servidor");
        alert.setMessage("Enviar mensaje al servidor");

        // Set an EditText view to get user input 
        final EditText input = new EditText(this);
        alert.setView(input);

        alert.setPositiveButton("Enviar", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          String value = input.getText().toString();
          MessageRequest msqr = new MessageRequest();
          msqr.msg = new Packets.Message();
          msqr.msg.lidfrom = pi.getPhoneLoginIdentifier();
          msqr.msg.lidto = new LoginIdentifier("SERVER","SERVER","-1");
          msqr.msg.msg = value;
          setTime(msqr.msg);
          send(msqr);
          }
        });

        alert.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
             dialog.dismiss();
          }
        });
        try{
            alert.show();
        }catch(Exception e){
        }

        break;
    }
    return true;
}
Trein
  • 3,658
  • 27
  • 36
Cako
  • 396
  • 3
  • 15
  • 1
    Please look here: [http://stackoverflow.com/questions/2092951/how-to-close-android-application][1] [1]: http://stackoverflow.com/questions/2092951/how-to-close-android-application – Ezzored Oct 18 '13 at 17:24
  • 4
    Please do not use `System.exit(0)`. Please stop your threads using appropriate methods on `Thread`. – CommonsWare Oct 18 '13 at 17:25
  • do you really need `System.exit(0)`. check the answer by commonsware http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon – Raghunandan Oct 18 '13 at 17:25
  • How can I stop an anonymous thread? – Cako Oct 18 '13 at 17:26
  • @Cako you don't need this `android.os.Process.killProcess(android.os.Process.myPid())` – Raghunandan Oct 18 '13 at 17:31
  • 2
    "How can I stop an anonymous thread?" -- hold onto the `Thread` object. Then, do something to cause the thread to shut down (e.g., `interrupt()`, flip an `AtomicBoolean` that you are monitoring, post a `ShutdownEvent` to the `LinkedBlockingQueue` that you are monitoring). – CommonsWare Oct 18 '13 at 17:41
  • ok.I have tried the first link,but the application continues restarting – Cako Oct 18 '13 at 19:10

1 Answers1

0

Finally I solved it by stopping the thread.Thanks to all

Cako
  • 396
  • 3
  • 15