我試圖運行警報,使主屏幕顯示警報對話框運行。即當應用程序關閉時,以及到達事件時間時,警報對話框將顯示事件的標題並無限響鈴。如果在警告對話框中點擊按鈕,然後警報響起,我的代碼如下:如何在主屏幕中顯示警報對話框?
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
任何幫助表示高度讚賞和感謝提前...
你可以像一個對話框主題活動,更在這裏:http://stackoverflow.com/questions/3167843/how-does-froyo-display-a-dialog-atop-the-home-screen – Tomik
我越來越例外: android.view.WindowManager $ BadTokenException:無法添加窗口 - 標記null不適用於應用程序 –