How do I make the AlertDialog box appear outside the app?
Asked Answered
C

3

0
@Override
public void run() {
    //Create thread that can alter the UI
    AlarmPage.this.runOnUiThread(new Runnable() {
        public void run() {
            cal = Calendar.getInstance();
            //See if current time matches set alarm time
            if((cal.get(Calendar.HOUR_OF_DAY) == alarmTime.getCurrentHour()) 
                    && (cal.get(Calendar.MINUTE) == alarmTime.getCurrentMinute())){
                //If the sound is playing, stop it and rewind
                if(sound.isPlaying()){
                    ShowDialog();
                    alarmTimer.cancel();
                    alarmTask.cancel();
                    alarmTask = new PlaySoundTask();
                    alarmTimer = new Timer();
                    alarmTimer.schedule(alarmTask, sound.getDuration(), sound.getDuration());
                }
                sound.start();
            }       
        }
    });
}

public void ShowDialog() {
    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    alertDialog.setTitle("REMINDER!");
    alertDialog.setMessage("Turn off alarm by pressing off");

    alertDialog.setNegativeButton("Off", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_SHORT);
        }
    });

    alertDialog.show();
}

I am making a simple alarm clock app that notifies the user. I want to make a alert box that gives the user the option to turn off the alarm when it goes off. I was able to make the alert box, but it only appears in the app not outside of the app. I understand the app has to be in the background running. If I need to show more code or be more specific, just ask please.

Convertible answered 22/4, 2015 at 15:59 Comment(2)
You should use notification to alert the user when app is not in foreground.Scrivings
possible duplicate : #11081880Discontinuous
H
1

Add a line as:

public void ShowDialog() {
    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    alertDialog.setTitle("REMINDER!");
    alertDialog.setMessage("Turn off alarm by pressing off");

    alertDialog.setNegativeButton("Off", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_SHORT).show();
        }
    });

    alertDialog.show();
    // line you have to add
    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
}

check now.

Hemicellulose answered 22/4, 2015 at 17:19 Comment(4)
Please try to let the questioner know where this code is supposed to be inserted, and also provide some commentary on what this code is doing and why.Ringleader
I know where to put it, problem is that getWindow() isn't working, "Cannot resolve".Convertible
@Ravi Koradiya i changed some stuff and it works, problem is it doesn't show it outside of the code, got any ideas?Convertible
getWindow doesn't exist. please explain your solution alsoOhg
I
1

Do not accept answers if they don't address your question, it is misleading.

The accepted answer is not correct, as it will never work outside your application.

Reason:

  1. It requires an activity context not application context.

  2. If you provide application context, your app will crash with IllegalArgumentException- you need to use Theme.AppCompat or their decendents...

If you need functionality as actually stated in the question you have to have a separate activity themed as a Dialog like here

or you can add a custom view to your window using window manager and making it system level alert like here.

Impolite answered 7/9, 2016 at 7:45 Comment(1)
I don't understand why this is not the accepted answerZebedee
F
0

Do this create an Activity without ContentView or a View associated with it and call your alertDialog method in your onCreate also remember to set the background of the Activity to Transparent using ColourDrawable

And that activity will look like a dialog or will suit your preference, you can also fall back to Themes so you can set an Activity as Dialog and treat it like Dialog also use DialogFragment

Favouritism answered 22/4, 2015 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.