编程语言
首页 > 编程语言> > java – 如何让AlertDialog框出现在应用程序之外?

java – 如何让AlertDialog框出现在应用程序之外?

作者:互联网

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

我正在制作一个通知用户的简单闹钟应用程序.我想制作一个警告框,让用户可以选择在闹钟响起时关闭闹钟.我能够制作警报框,但它只出现在不在应用程序之外的应用程序中.我知道应用程序必须在后台运行.如果我需要显示更多代码或更具体,请询问.

解决方法:

添加一行:

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

现在检查.

标签:android-alertdialog,android,java,android-alarms
来源: https://codeday.me/bug/20190926/1820671.html