每天没有收到本地警报通知

时间:2018-06-20 06:50:30

标签: java android notifications android-broadcastreceiver

我尝试设置AlarmManager.setRepeating()状态,但没有成功, 我尝试在昨天上午10点设置“警报”,但仅收到当天的警报通知,第二天上午10点没有任何通知。
以下是我的代码,请让我知道我的代码出了什么问题。

Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(ReminderActivity.this,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
long timeInMillis = System.currentTimeMillis();
Log.i(TAG, "timeInMillis: " + timeInMillis);
Logger.addRecordToLog(ReminderActivity.this,"hours "+hour +"minutes "+minute,"triggered "+new Date());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Log.i(TAG, "hour: " + hour);
Log.i(TAG, "minute: " + minute);
Log.i(TAG, "" + calendar.getTimeInMillis());
Logger.addRecordToLog(ReminderActivity.this,"calendar Alaram Time"+calendar.getTimeInMillis(),"triggered "+new Date());


alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

**************************** BroadCast接收器类*************** < / p>

public class AlarmReceiver extends BroadcastReceiver {
    private final static String TAG = "AlarmReceiver";

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

        Log.i(TAG, "onReceive()");
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String current_date = dateFormat.format(date); 
        Logger.addRecordToLog(context,"AlarmReceiver","Received daily cup reminder "+current_date);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_daily_cup)
                        .setContentTitle(context.getString(R.string.daily_cup_reminder))
                        .setContentText(context.getString(R.string.daily_cup_message))
                        .setAutoCancel(true);

        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(pendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    }
}

1 个答案:

答案 0 :(得分:0)

尝试

Calendar calendar = Calendar.getInstance();
                    if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 10) {
                        calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
                    }
                    calendar.set(Calendar.HOUR_OF_DAY, 10);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

                    alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
                            AlarmManager.INTERVAL_DAY, pi);

尝试设置适当的triggerAtTime(将来)-

Calendar.getInstance().getTimeInMillis()+(24*60*60*1000)