2011-09-22 104 views
1

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

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: Unable to add window -- token null is not for an application

任何帮助表示高度赞赏和感谢提前...

+0

你可以像一个对话框主题活动,更在这里:http://stackoverflow.com/questions/3167843/how-does-froyo-display-a-dialog-atop-the-home-screen – Tomik

+0

我越来越例外: android.view.WindowManager $ BadTokenException:无法添加窗口 - 标记null不适用于应用程序 –

回答

3

当您收到警报通知,那么你可以在设有活动显示警告对话框没有开始活动的视图。这是你想要的。?

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

+0

但是,如果我们关闭了应用程序,那么警告对话框将无法显示,因为它仅适用于应用程序 –

+0

你的应用程序关闭,你也可以通过你的闹钟在BroadcastReceiver中得到通知,并从那里你可以通过活动显示对话框。 –

+0

但我得到以下错误: 构造函数AlertDialog.Builder(AlarmReceiver)是未定义的 –

相关问题