有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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


共 (2) 个答案

  1. # 1 楼答案

    这样做可以创建一个没有关联的ContentViewViewActivity方法,并在onCreate中调用alertDialog方法。还记得使用ColourDrawableActivity的背景设置为Transparent

    该活动看起来像一个对话框,或者符合您的偏好,您也可以返回到Theme,这样您就可以将Activity设置为Dialog,并将其视为Dialog也可以使用DialogFragment

  2. # 2 楼答案

    添加一行,如下所示:

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

    现在检查一下