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();
}
我正在制作一个简单的闹钟应用程序,通知用户。我想制作一个警报框,让用户在警报关闭时可以选择关闭警报。我能够创建警报框,但它只出现在应用程序中,而不是在应用程序之外。我知道应用程序必须在后台运行。如果我需要显示更多代码或更具体,请询问
# 1 楼答案
这样做可以创建一个没有关联的
ContentView
或View
的Activity
方法,并在onCreate
中调用alertDialog
方法。还记得使用ColourDrawable
将Activity
的背景设置为Transparent
该活动看起来像一个对话框,或者符合您的偏好,您也可以返回到
Theme
,这样您就可以将Activity
设置为Dialog
,并将其视为Dialog
也可以使用DialogFragment
# 2 楼答案
添加一行,如下所示:
现在检查一下