2017-11-07 5 views
1

버튼을 누르면 내 알림 팝업이 나타 났지만 버튼을 누른 후 X 분 (또는 Y 시간)이 경과 할 때까지 알림이 표시되지 않도록 추가 개발하고 싶습니다. .X 분 후에 알림을 표시하도록 설정하는 방법은 무엇입니까?

관련 코드 :

private void sendNotif() { 
    notification.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.avatar)); 
    notification.setTicker("Kayla has returned"); 
    notification.setWhen(System.currentTimeMillis()); 
    notification.setContentTitle("Kayla has returned"); 
    notification.setContentText("See what news she has to share"); 

    Intent notifIntent = new Intent(this, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
    notification.setContentIntent(pendingIntent); 

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    nm.notify(notifId, notification.build()); 
} 

private void setNotif() { 
    Button btn = (Button) findViewById(R.id.notifBtn); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      sendNotif(); 
     } 
    }); 
} 

내가 어떤 방법으로 알람 관리기를 사용해야합니다,하지만 난 통지에 통합 할 수없는 것 냈어요 ...

+0

는'AlarmManager'는 의도를 발생 설정된 시간 (또는 사용 방법에 따라 달라질 수 있습니다.) 'BroadcastReceiver'를 사용하여 그 의도를 처리하고 그 의도를 알려주십시오. – Kuffs

+0

사례를 보여줄 수 있습니까? – PQuix

+0

https://stackoverflow.com/a/7960057/1027277 – Kuffs

답변

0

당신이 원하는 미래의 특정 시간으로 설정하십시오. 예를 들어, 현재 오후 12:00시에 10 분 내에 원한다면 12:10 PM으로 알람을 설정하십시오. 그래서 지금은 항상 현재 시간 + 알람이 울릴 때까지 기다리기를 원하는 시간을 얻습니다. 다음은 그 예입니다. 나는 20 분마다 그 알람을 반복하기를 원한다면 포함시켰다.

private AlarmManager alarmMgr; 
private PendingIntent alarmIntent; 

private void setAlert(int hour, int minute) { 
    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(context, AlarmReceiver.class); 
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, hour); 
    calendar.set(Calendar.MINUTE, minute); 

    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
    1000 * 60 * 20, alarmIntent); 
} 

보내기 알림 : 현재

하는 수신기에서 컨텍스트를 얻을 수 있는지 확인하십시오 당신의 축배를 요구하고있다

public void showNotification(Context context) { 

    NotificationCompat.Builder mBuilder = 
    new NotificationCompat.Builder(context) 
    .setSmallIcon(R.drawable.notification_icon) 
    .setContentTitle("Notification Title") 
    .setContentText("Notification Text"); 

    NotificationManager mNotificationManager = 
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    NotificationManager.notify(). 

    mNotificationManager.notify(001, mBuilder.build()); 
} 

통화 sendNotification에는

@Override 
public void onReceive(Context context, Intent arg1) { 
    showNotification(context); 
} 
+0

대단히 감사합니다! AlarmReceiver 클래스의 토스트만으로 작업 할 수 있지만 알림을위한 코드를 구현하려고하면 작동하지 않는 것 같습니다. 제가 그 부분을 어떻게 구현할 수 있을지 제안 해 주시겠습니까? – PQuix

+0

내 대답 – TomD

+0

에 코드를 추가했습니다. 바보 같은 질문에 대해 미안하지만 sendAutivity 메소드를 내 MainActivity로 가져 와서 MyAlarmReceiver 클래스에서 호출하거나 MyAlarmReceiver에서 메소드를 작성합니까? 왜냐하면 내가 그들 중 하나를 일할 수 없기 때문에 ... 내가 MainActivity에 메서드를 넣으면 MyAlarmReceiver의 onReceive 메서드에서 호출 할 수 없게되고 MyAlarmReceiver에서 메서드를 만들려고하면 "getSystemService (java.lang.String) '메서드를 확인할 수 없습니다.' – PQuix