我想在每日特定时间设置闹钟。我已经写了这段代码也将在第二天工作。谢谢你提前

时间:2017-08-24 09:31:38

标签: android alarmmanager

我想在每天上午10:45设置闹钟,现在这个代码问题是setRepeat()中提供的时间间隔不起作用,如果我也放5 * 1000即5秒。帮助

public void SetAlarm()
{

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override public void onReceive( Context context, Intent _ )
        {
            Toast.makeText(context, "hi", Toast.LENGTH_SHORT).show();
            tex.setText(s[i+1]);
            i++;

            Notification.Builder n  = new Notification.Builder(context)
                    .setContentTitle("Today's Quote")
                    .setContentText(tex.getText())
                    .setSmallIcon(R.drawable.ic_lightbulb_outline_black_24dp)
                    .setContentIntent(PendingIntent.getActivity(context,0,new Intent(""),0))
                    .setSound(uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setAutoCancel(true);


            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(1, n.build());

            context.unregisterReceiver( this ); // this == BroadcastReceiver, not Activity
        }
    };

    this.registerReceiver( receiver, new IntentFilter("com.blah.blah.somemessage") );

    PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent("com.blah.blah.somemessage"), 0 );
    AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 10);
    calendar.set(Calendar.MINUTE, 45);
 manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,Calendar.getInstance().getTimeInMillis()+AlarmManager.INTERVAL_FIFTEEN_MINUTES,AlarmManager.INTERVAL_DAY, pintent);
}

1.这将首先在11:00发出警报 它会每天发出警报吗?

1 个答案:

答案 0 :(得分:0)

使用

manager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000, pi); 

而不是

manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,Calendar.getInstance().getTimeInMillis()+AlarmManager.INTERVAL_FIFTEEN_MINUTES,AlarmManager.INTERVAL_DAY, pintent);