2014-08-29 5 views
0

특정 시간에 알림을 보내고 싶습니다. 아래는 제 코드입니다. 지금은 앱을 시작할 때만 알림을 받지만 지정된 시간에 알림을받지 못합니다. 내가 뭘 잘못하고 있는지 이해할 수있게 도와 주시겠습니까?특정 시간에 알림을 보내는 방법은 무엇입니까?

NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("My notification") 
      .setContentText("Trainieren sie jetzt!") 
      .setAutoCancel(true); 
    Intent resultIntent = new Intent(this, MainActivity2.class); 

    AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE); 



    PendingIntent resultPendingIntent = 
     PendingIntent.getActivity(
     this, 
     0, 
     resultIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 


    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.HOUR_OF_DAY, 12); 
    cal.set(Calendar.MINUTE, 54); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24*60*60*1000, resultPendingIntent); 



    mBuilder.setContentIntent(resultPendingIntent); 
    int mNotificationId = 001; 
    NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

감사합니다.

@Override 
public void onReceive(Context context, Intent intent) { 
    // Toast.makeText(MainActivity.con, "Hello from alarm", 1).show(); 


    // System.out.println("Hello from Alarm"); 
    context.startService(new Intent(context, NotificationService.class)); 

} 

을 위의 예에서 나는 웨이크 잠금 및 배경 서비스 구현을 관리, 내 통지를 처리하기 위해 내 서비스 클래스에 과정을 감독하고 있습니다 :

+0

시간을 설정하는 곳에서 알림을 설정해야합니다 ... – Umair

+1

정말 반복 알람을 원하십니까? – Opiatefuchs

+0

브로드 캐스트 수신기로 반복 알람을 설정하는 것은 내가 생각할 수있는 한 병목이 아닙니다. 타이머 옵션 만이 매우 복잡 할 수 있습니다. 웨이크 로크가 서비스 내에서 적절히 구현된다면 더욱 효율적으로된다. – Skynet

답변

1

당신은 추가해야합니다. 서비스를 구현하지 않으면 onRecieve 메소드에 알림 만 표시 할 수 있습니다.

here의 알람 클래스를 완벽하게 구현했습니다.

관련 문제