Don't Be negative
Don't Be negative

Reputation: 1215

Notification is not closing with app android?

I followed RadioPlayerService as a example to Create radio stream in android

I have added the below code to exit in RadioActivity.java

@Override
    public void onBackPressed() {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Exit ! Radio App.");
        alertDialogBuilder
                .setMessage("Click Yes to Exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                moveTaskToBack(true);
                                android.os.Process.killProcess(android.os.Process.myPid());
                                System.exit(0);

                            }
                        })

                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();

    }

Here My app is exiting but the notification is working this is my notification class

Can any one suggest me How to kill entire app on exit

Or combine both notification service and main activity so that I can exit the app completely

This is the full reference code

Can any one suggest me on this kind

Update 1

I am giving exit option in main activity not in other service my main activity is RadioActivity.java and my service is RadioPlayerService.java

So here I am giving Exit to main activity so I what i need means instead of exiting main activity It should exit entire app including service.

Don't down-vote without comment don't comment without seeing full sourcode ..

Thanks again

Upvotes: 0

Views: 1691

Answers (2)

Maveňツ
Maveňツ

Reputation: 1

You have used System.exit(0); , I would like suggest you not to use it. It is not a good programming style to use it. There is a method called finish();

  • You don't need to call System.exit(0) to terminate an Activity, just Activity.finish()

Close an application

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);

You can make your activity as Single instance so that from anywhere you call the device will return the same activity from the stack.

In Manifest.xml

" ... android:launchMode="singleInstance">

To avoid an Intent to call multiple instance of the same Activity remove the FLAG_ACTIVITY_NEW_TASK flag from the Intent and add the FLAG_ACTIVITY_CLEAR_TOP one.

From Doc:

FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

FLAG_ACTIVITY_NEW_TASK
If set, this activity will become the start of a new task on this history stack.


Clear all notification

NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();

Update1

@Override
public void onBackPressed() {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("Exit ! Radio App.");
    alertDialogBuilder
            .setMessage("Click Yes to Exit!")
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            mRadioManager .disconnect();// after adding this its also closed radio player
                            NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                            nMgr.cancelAll();
                            Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                            homeIntent.addCategory( Intent.CATEGORY_HOME );
                            homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(homeIntent);

                        }
                    })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();

}

Upvotes: 2

Hasif Seyd
Hasif Seyd

Reputation: 1694

so basically what you can do is, when you click yes on your dialog, you should kill your activity by calling finish(), and also you have to stop your service, or if it is binded by your Activity, you can avoid calling stopService and it will automatically unbind when your activity is destroyed by calling Finish(), check the code below

   AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Exit ! Radio App.");
        alertDialogBuilder
                .setMessage("Click Yes to Exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                moveTaskToBack(true);
                                //this will unbind the radioService
                                mRadioManager.disconnect();
                                finish();

                            }
                        })

                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();

and in your RadioPlayerService , override onDestroy method and call the public method stopFromNotification() method also

Upvotes: 1

Related Questions