简体   繁体   English

如何使AlertDialog框出现在应用程序外部?

[英]How do I make the AlertDialog box appear outside the app?

@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. 如果我需要显示更多代码或更具体,请问一下。

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. 现在检查。

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... 如果提供应用程序上下文,则您的应用程序将因IllegalArgumentException而崩溃- 您需要使用Theme.AppCompat或其后代...

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 . 或者,您可以使用窗口管理器向窗口中添加自定义视图,并使其像此处一样成为系统级警报。

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 这样做会创建一个没有ContentView或与之关联的ViewActivity ,并在onCreate调用您的alertDialog方法时还记得使用ColourDrawableActivity的背景设置为Transparent

And that activity will look like a dialog or will suit your preference, you can also fall back to Theme s so you can set an Activity as Dialog and treat it like Dialog also use DialogFragment 而且该活动看起来像一个对话框或适合您的喜好,您还可以退回到Theme因此您可以将Activity设置为Dialog ,并将其像Dialog一样对待,也可以使用DialogFragment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM