0

i am working on a tutorial on service in this ..i was able to make it run perfectly what i did try is to start service on Oncreate and it did and make it stop on back button but unfortunately onstop does not work because it is not showing toast and log here is my code..what causes onstop not to work

public void onBackPressed() {


        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

        builder.setTitle("Confirm Close");

        builder.setMessage("Are you sure you want to exit?");

        builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                stopService(new Intent(this, MyService.class));
                System.exit(0);
                finish();
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog alertdialog=builder.create();
        alertdialog.show();
    }

service code

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service{

    private static final String TAG = "MyService";

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

    @Override
    public void onCreate() {
        Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    //Note: You can start a new thread and use it for long background processing from here.
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }
}
Giant
  • 1,619
  • 7
  • 33
  • 67
  • Post the logcat error. – Siddharth_Vyas Jul 07 '14 at 07:57
  • Just call `finish()` instead of `System.exit(0);finish(); android.os.Process.killProcess(android.os.Process.myPid());` – Siddharth_Vyas Jul 07 '14 at 07:58
  • @SiddharthVyas no error just that no logcat and toast there should be logcat and toast because of service...i need those because i want to terminate the app when back button is pressed.. – Giant Jul 07 '14 at 08:00
  • @SiddharthVyas sir can i ask you your code worked but will it terminated my app if i remove System.exit(0);finish(); android.os.Process.killProcess(android.os.Process.myPid());? – Giant Jul 07 '14 at 08:18
  • 1
    @HakHak Yes, it will terminate the app. You don't need anything except `finish()` – Sergey Glotov Jul 07 '14 at 08:23
  • 1
    @HakHak : Yes definately. – Siddharth_Vyas Jul 07 '14 at 08:25
  • @HakHak You just terminate your service with `System.exit(0)` and `killProcess()`, so service don't have time to do anything in `onDestroy()`. You can read the thread about `killProcess()`: http://stackoverflow.com/questions/11035328/why-calling-process-killprocessprocess-mypid-is-a-bad-idea – Sergey Glotov Jul 07 '14 at 08:30
  • @SergeyGlotov meaning to say that my code above really did work?it stopped the service its just that it didnt bother giving the toast or logcat?and when i remove those lines my project didnt really stop the service but the project only? – Giant Jul 07 '14 at 08:44
  • 1
    @HakHak Yes, it works and stops service and then everything else in your app. When you remove these lines everything works too: it stops the service and the app, but in the 'system' way, as it should be. Please, don't use these methods while you don't understand them. Read this thread, at least: http://stackoverflow.com/questions/11035328/why-calling-process-killprocessprocess-mypid-is-a-bad-idea – Sergey Glotov Jul 07 '14 at 08:50
  • @SergeyGlotov i have read it although i dont fully understand it i do know that when i used back button in my example i did not interrupt anything it just ending the app terminating all processes.thank you for clearing this out for me sir. – Giant Jul 07 '14 at 08:52

1 Answers1

1

Replace your code by following code

    public void onBackPressed() {


            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

            builder.setTitle("Confirm Close");

            builder.setMessage("Are you sure you want to exit?");

            builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    stopService(new Intent(this, MyService.class));
                   Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alertdialog=builder.create();
            alertdialog.show();
        }
Shyam
  • 6,376
  • 1
  • 24
  • 38
  • removing System.exit(0);finish(); android.os.Process.killProcess(android.os.Process.myPid()); did the trick sir but + 1 for your effort.. – Giant Jul 07 '14 at 08:27