2

So, I'm trying to create an app which have a function like an alarm clock for Android phones, it will alert the user at their chosen time. When the alert screen show up, the user has an option to push a notification or snooze, and the app will push a notification and then close.

However, when I click the snooze or the notification button, the app did not close. Instead, the app stop the alarm sound and minimized (just as when the user touch the home button). If I touch the recent app, I then can open the alert screen again.

Edit: I know that there're some codes such as System.exit() or killProcess, since those code are not recommended, I prefer avoid using them. The reason I ask is because I tested the real clock app that come with my phone (4.3), and the it's alert screen will close after the I press the snooze or dismiss button. So there must be a way for me to do the same, right ?

Answer Okay, so as Sagar Pilkhwal explained below, and after reading others related problems, I found out that there's no "good" method to close you app by codes, you have to leave that option to the users or OS. Unless you want to use System.exit or killProcess, but they're bad ways to force your app to close.

Sagar Pilkhwal also have a alternatively method to this problem, if you don't want your alert screen show up in recent app, you bring up another activity.

However, as for this case and Alarm Manager in general, open MainActivity will lead to nasty stuffs, since MainActivity is when we handle Alarm events. Then I found out another solution, if you don't want your alert screen show up in recent app, simply add

android:excludeFromRecents="true"

to your Alert.class Activity in the Manifest, or add Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTSto the intent used to start Alert.class.

This will lead to other problem, as when the user press the Home button, the alarm won't turn off, and the user can't open recent app to access the alert screen (he'll has to open the app to do so). I fix this problem by try to detect the home button pressed event. Detail answer are below.

Community
  • 1
  • 1
Usin2705
  • 43
  • 1
  • 6
  • Consider making a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). This is a lot of code, and not all of it is necessary to reproduce your problem. It would be helpful to include the language you programmed this in and what device(s) you are programming for. – Will Beason Sep 04 '14 at 18:29
  • Thank you, I edited and remove some of those codes. For the rest, I really don't know whether it relevant to the problem or not (if I know I then I wouldn't ask this question), so to be safe I still keep it. – Usin2705 Sep 05 '14 at 05:08

3 Answers3

1

well im thinknig.

System.exit(0);

maybe? im new to android thou..but i sometimes use it..

  • 1
    Do not ever use `System.exit(0)`. That goes against the Android coding practices. It's designed to quit the app immediately which is not what you want. An alternative is to use the finish() method if you have a reason to leave the app pre-maturely. What will happen when `System.exit(0)` execute? Ans: The VM stops further execution and program will be exit. – Sagar Pilkhwal Sep 04 '14 at 18:49
  • oh ok.. so that is meant for brutal force finish right? so services and receivers are also goin to be killed?? right?? @SagarPilkhwal –  Sep 04 '14 at 18:59
  • Check this [post](http://stackoverflow.com/a/18802574/3326331) which says there are issues with *AdMob* when `System.exit(0);` is used. – Sagar Pilkhwal Sep 04 '14 at 19:03
1

Put the following to Manifest, in your Alert activity :

android:excludeFromRecents="true" //this will make the Activity be exclude from recents list.

You can also add android:launchMode="singleInstance" and android:taskAffinity="" (if you know what they do)

In the Alert class, use this to detect home button pressed event:

@Override
public void onPause() {
    if (!isFinishing()) {
        createNotf(); //Handle home button press here.
    }
    super.onPause();
}

Use this to handle back button pressed: (You have to have this code if you want to detect home button press using isFinishing();

@Override
public void onBackPressed() {
    //Handle BackButton event here.
}

For my case only, when the user press home button, I will create a notification, so he can either click on the notification or open the app again to access to Alert Screen.:

private void createNotf() {
    Intent screenIntent = new Intent(MyAlert.this, MyAlert.class);
    screenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    screenIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    PendingIntent pIntent = PendingIntent.getActivity(MyAlert.this, MainActivity.SEND_ALARM_CODE, screenIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    Notification.Builder ntfBuilder = new Notification.Builder(this)
    .setAutoCancel(true)
    .setContentTitle("ALARM_RUNNING")
    .setLargeIcon(bitmap)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentText("CLICK_TO_OPEN_ALERTSCREEN.")
    .setContentIntent(pIntent);

    NotificationManager myNotfM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);     
    myNotfM.notify(999, ntfBuilder.build());        
}

When the Alert stop, that notification will also be clear:

@Override
public void onDestroy() {
    super.onDestroy();
    NotificationManager mntfM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mntfM.cancel(999);}
Usin2705
  • 43
  • 1
  • 6
0
System.exit(0);

That right there will close your application out leaving nothing running in the background.However,use this wisely and don't leave files open, database handles open, etc.These things would normally be cleaned up through the finish() command.

I personally HATE when I choose Exit in an application and it doesn't really exit.

OmerFaruk
  • 292
  • 2
  • 13