java rtc alarm,Java AlarmManager.setAlarmClock方法代码示例

import android.app.AlarmManager; //导入方法依赖的package包/类

private void addAlarm(Context context, GregorianCalendar date) {

int notificationId = new TimelessDate(date).hashCode();

Log.d("ALARM", "Scheduling alarm " + notificationId);

PendingIntent p = PendingIntent.getBroadcast(

context,

notificationId,

new Intent(context, AlarmReceiver.class),

PendingIntent.FLAG_UPDATE_CURRENT);

try {

AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

if (manager != null) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

manager.setAlarmClock(new AlarmManager.AlarmClockInfo(date.getTimeInMillis(), p), p);

} else {

manager.setWindow(AlarmManager.RTC_WAKEUP,

date.getTimeInMillis(),

1,

p);

}

serializeAlarm(context, notificationId);

}

} catch (SecurityException e) {

StringWriter sw = new StringWriter();

PrintWriter pw = new PrintWriter(sw);

e.printStackTrace(pw);

String errMSG = e.getMessage() + "\n" + sw.toString();

e.printStackTrace();

Activity act = ActivityHelper.getActivity();

if (act != null) {

ErrorDialog.newInstance("ERROR", "Failed to schedule alarm. Did some alarms crash?", errMSG)

.show(act.getFragmentManager(), "ALSECERROR");

}

}

Log.d("ALARM", "Alarm ready for "

+ " " + DateUtilities.GERMAN_STD_STIMEFORMAT.format(date.getTime())

+ DateUtilities.GERMAN_STD_SDATEFORMAT.format(date.getTime()));

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值