2015-01-16 4 views
3

버튼을 클릭하자마자 알림 표시 줄에 메시지를 보내는 Notification.Builder이 있습니다. 선택한 시간에 알림을 표시하는 방법이 있습니까? Android 설명서를 살펴 보았지만 제대로 작동하는 것처럼 보였습니다.Android : 특정 시간에 Notification.Builder 실행

 Notification n = new Notification.Builder(this) 
      .setContentTitle("Random title") 
      .setContentText("Random text") 
      .setSmallIcon(R.drawable.abc_ic_go_search_api_mtrl_alpha) 
      .setContentIntent(pIntent).build(); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    notificationManager.notify(0, n); 

감사 :

여기 내가 가지고있는 코드입니다!

답변

4

1) 알림 코드가있는 브로드 캐스트 수신기를 만듭니다.

public class AlarmReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification notification = new NotificationCompat.Builder(context) 
       .setContentTitle("Random title") 
       .setContentText("Random text") 
       .setSmallIcon(R.drawable.abc_ic_go_search_api_mtrl_alpha) 
       .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0)) 
       .build(); 

     notificationManager.notify(0, notification); 
    } 
} 

2) AlarmManager를 사용하여 알람을 예약하여 브로드 캐스트 리시버의 인 텐트를 브로드 캐스트합니다.

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
Intent intent = new Intent(this, AlarmReceiver.class); 
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 
// set for 30 seconds later 
alarmMgr.set(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis() + 30000, alarmIntent); 
+0

을 보여 주지만, 그것이 작동하지 않은 곳이 있습니다. – ruselli

1

사용하면 알림을 설정하기

//Create an offset from the current time in which the alarm will go off. 
Calendar cal = Calendar.getInstance(); 
cal.add(Calendar.SECOND, 15); 

//Create a new PendingIntent and add it to the AlarmManager 
Intent intent = new Intent(this, MyAlarmReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 
     100, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent); 

을 원하는 그리고 여기에 알림 죄송합니다

public class MyAlarmService extends Service 
{ 

    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     return null; 
    } 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     super.onStart(intent, startId); 
     // put notification code here 
    } 

    @Override 
    public void onDestroy() 
    { 
     super.onDestroy(); 
    } 
}