2017-12-18 1 views
-3

사용자가 알림을받지 않는 한 알림은 사용자에게 5 분마다 전송되므로 알림을 반복하고 싶습니다. 그리고 그가 그럴 때 경보를 취소하고 싶습니다. 알림을 5 분마다 보내는 방법은 무엇입니까?

내 코드,하지만 난 알람 매니저와 달력과 함께 할 무엇 확실하지 않다 :

acceptBtnPendingIntent = PendingIntent.getBroadcast(this, 123, acceptBtnIntent, 0); 
remoteView = new RemoteViews(this.getPackageName(), R.layout.notification_layout); 
remoteView.setOnClickPendingIntent(R.id.acceptCallBtn, acceptBtnPendingIntent); 
remoteView.setImageViewResource(R.id.notificationIcon, R.drawable.notification_icon); 
remoteView.setTextViewText(R.id.fromTxt, fromId); 

Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000 * 60 * 20, acceptBtnPendingIntent); 

mBuilder = new NotificationCompat.Builder(this); 
mBuilder.setSmallIcon(R.mipmap.ic_launcher) 
    .setCustomHeadsUpContentView(remoteView) 
    .setDefaults(Notification.DEFAULT_ALL) 
    .setContentIntent(acceptBtnPendingIntent) 
    .setAutoCancel(true) 
    .setPriority(NotificationManager.IMPORTANCE_HIGH); 
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(notificationsId, mBuilder.build()); 
+0

작업 스케줄러를 사용하십시오. https://medium.com/google-developers/scheduling-jobs-like-a-pro-with-jobscheduler-286ef8510129 – DroiDev

+0

SO는 코드 작성 서비스가 아닙니다. 지금까지 시도한 방법을 제시해야합니다. – JaredH

답변

0

희망이 귀하의 경우에 작업 할 수 있습니다.

//write the code where you would like to create and call alarm repetitively 

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    Intent intent = new Intent(context,AlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234/*unique request code*/, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),5 * 60 * 1000 /*5 min in millis*/, pendingintent); 

//your Broadcast Receiver 
public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG"); 
     //Acquire the lock 
     wl.acquire(); 
showNotification(context); 

    if (intent.getAction().equals("USER_ACCEPTED")) { 

    //TODO: do your action you would like to do: do cancellation of alarm.. code here! 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     Intent intent = new Intent(context,AlarmReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234/*unique request code*/, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
      alarmManager.cancel(pendingIntent); 
      pendingIntent.cancel(); 

     } 

} 

public void showNotification(){ 

mBuilder = new NotificationCompat.Builder(this); 
mBuilder.setSmallIcon(R.mipmap.ic_launcher) 
    .setDefaults(Notification.DEFAULT_ALL) 
    .setAutoCancel(true) 
    .setPriority(NotificationManager.IMPORTANCE_HIGH); 
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(notificationsId, mBuilder.build()); 

} 

내가 도움이 될만한 코드를 제공했습니다. 내가 어딘가에 잘못되었다는 사실을 알려주십시오.

관련 문제