[英]How to show alert dialog in home screen?
我正在尝试运行警报,使警报对话框在显示的主屏幕中运行。 例如,当应用程序关闭时,以及到达事件时间时,警报对话框将显示事件的标题并无限响警报。 如果单击警报对话框中的按钮,则警报将关闭,我的代码如下:
void doReminderWork(Intent intent) {
ToDoApplicationActivity ap = new ToDoApplicationActivity();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, ToDoApplicationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note = new Notification(R.drawable.alarm, "...Calendar Alarm...", System.currentTimeMillis());
note.setLatestEventInfo(this,"Event : ", ap.title1, pi);
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
int id = 123456789;
manager.notify(id, note);
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(this, alert);
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0)
{
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(false);
try
{
mMediaPlayer.prepare();
}
catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
mMediaPlayer.start();
}
此后,如果我给出警报对话框代码来停止媒体播放器,我知道它无法停止播放警报。 在这里,即使关闭了应用程序,我也需要显示警报对话框。 我已经尝试了2天了。
我正在关注异常:
android.view.WindowManager $ BadTokenException:无法添加窗口-令牌null不适用于应用程序
非常感谢您的任何帮助,并在此先感谢...
当您收到警报通知时,可以通过启动活动在没有视图的活动中显示警报对话框。 这就是你想要的。
通过在活动中显示警报而没有视图,您将在主屏幕上仅看到警报对话框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.