2017-10-30 10 views
0

알림을 특정 시간에 설정하려고하지만 알림이 정확한 시간에 표시되지 않습니다.알람 관리자 및 브로드 캐스트 수신기가 정확한 시간에 알림을 표시하지 않음 - Android

간혹 정확한 시간에 알림이 나타나고 때로는 10-15 초의 지연이 있습니다.

private void showNotification(long lastScratched) { 

     Intent intent = new Intent(getActivity(), NotificationReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     lastScratched = lastScratched + timeInterval; 

     AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, lastScratched, pendingIntent); 
     Log.e("MyNotification", "Next Notification Time : "+lastScratched+""); 
    } 

NotificationReceiver.java

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

     Intent myIntent = new Intent(context, MainActivity.class); 
     myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "my_channel_01") 
       .setContentIntent(pendingIntent) 
       .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon)) 
       .setSmallIcon(R.drawable.app_icon) 
       .setContentTitle("Notification Title") 
       .setContentText("Notification Text") 
       //.setPriority(Notification.PRIORITY_MAX) 
       .setSound(alarmSound) 
       .setAutoCancel(true); 

     notificationManager.notify(100, builder.build()); 
    } 
} 

답변

1

사용 setExactAndAllowWhileIdle 또는 JobSchreduler.

setExactAndAllowWhileIdle를 들어 다음을 사용할 수 있습니다

AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
+0

setExactAndAllowWhileIdle와 setExact의 차이점은 무엇입니까? 어떤 하나는 setExact 또는 setExactAndAllowWhileIdle을 사용해야합니까? –

+1

** setExactAndAllowWhileIdle **은 Android가 절전 모드로 전환 될 때 절전 모드에서 작동합니다. – Ghost