0

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

Maveňツ
  • 1
  • 12
  • 50
  • 89
Don't Be negative
  • 1,215
  • 3
  • 19
  • 46

2 Answers2

2

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

}
Don't Be negative
  • 1,215
  • 3
  • 19
  • 46
Maveňツ
  • 1
  • 12
  • 50
  • 89
  • 1
    actually I am giving exit option to main activity itself radioactivty.java can you update your answer – Don't Be negative Dec 27 '16 at 06:00
  • 1
    @Don'tBenegative pls check the complete solution I have updated this may help. if u still have prb then comment here :-) #soReadyToHelp *don't downvote please* – Maveňツ Dec 27 '16 at 06:03
  • 1
    thanks @Manish Yadav sir...... your code worked nice but radio player is not closed but after adding this its exited every thing mRadioManager .disconnect(); – Don't Be negative Dec 27 '16 at 06:41
1

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

Hasif Seyd
  • 1,686
  • 12
  • 19
  • Here what is your_service_action in intent My notification is [this](https://github.com/iammert/RadioPlayerService/blob/master/library/src/main/java/co/mobiwise/library/radio/RadioPlayerService.java) can you update your answer – Don't Be negative Dec 27 '16 at 05:48
  • @Don'tBenegative can you tell me in your RadioActivity, how you are starting your RadioService or can you paste the link of your RadioActivity. i will update accordingly :) – Hasif Seyd Dec 27 '16 at 05:50
  • Please see the entire code that radio service is in library but in my code I combined both see full source code and upvote dont downvote plese without refering – Don't Be negative Dec 27 '16 at 05:56
  • @Don'tBenegative please check my answer, i have updated the code after reviewing your code shared , and i don't downvote on any post, don't worry :) – Hasif Seyd Dec 27 '16 at 06:04
  • @Don'tBenegative also override onDestroy in your RadioService class and call stopFromNotification method, this will fix everything – Hasif Seyd Dec 27 '16 at 06:07
  • thanks @Hasif Seyd sir In your answer this (`mRadioManager.disconnect(); finish();`) helped me lo lot – Don't Be negative Dec 27 '16 at 06:44