如何在主屏幕中显示警报对话框?

时间:2011-09-22 10:11:00

标签: java android

我正在尝试运行警报,使主屏幕显示警报对话框。即当应用程序关闭时,以及达到事件时间时,警报对话框会显示事件的标题并无限响铃。如果单击警告对话框中的按钮,则警报响起,我的代码如下:

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

     }

在此之后如果我提供警告对话框代码来停止媒体播放器,我知道它无法停止播放闹钟。在这里,即使应用程序关闭,我也需要显示警告对话框。我试了这两天。

我正在接受以下例外:

  

android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序

任何帮助都非常感谢并提前感谢...

1 个答案:

答案 0 :(得分:3)

当您收到警报通知时,您可以通过启动活动在没有查看的活动中显示警报对话框。这就是你想要的。?

通过在没有视图的活动中显示警报,您将在主屏幕上看到仅警报对话框。