2011-09-22 5 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 일 동안 이것을 시도하고있다. 당신이 알람 통지를받을 때 당신이 가지고있는 활동에 경고 대화 상자를 표시 할 수 있습니다

+0

대화처럼 테마 활동을 할 수 있습니다. 자세한 내용은 여기를 참조하십시오. http://stackoverflow.com/questions/3167843/how-does-froyo-display-a-dialog-atop-the-home-screen – Tomik

+0

예외 다음 : android.view.WindowManager $ BadTokenException : 창을 추가 할 수 없습니다 - 응용 프로그램에 대해 토큰 null이 없습니다. –

답변

3

... 어떤 도움을 매우 높이 평가하고 사전에 감사한다

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

:

나는 다음과 같은 예외를 받고 있어요 액티비티를 시작하여 뷰가 없습니다. 이게 니가 원하는거야.?

보기가없는 활동에 경고를 표시하면 홈 화면에 경고 대화 상자 만 표시됩니다.

+0

그러나 응용 프로그램을 닫으면 응용 프로그램 내에서만 작동하므로 경고 대화 상자를 표시 할 수 없습니다. –

+0

귀하의 응용 프로그램이 닫힌 다음 BroadcastReceiver에서 경보를 통해 통지를받을 수 있으며 거기에서 활동을 통해 대화 상자를 표시 할 수 있습니다. –

+0

하지만 다음과 같은 오류가 발생합니다 : 생성자 AlertDialog.Builder (AlarmReceiver)가 정의되지 않았습니다 –

관련 문제