2011-09-22 177 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)是未定義的 –

相關問題