无法在 android 的 AlramManager 中设置指定日期
Posted
技术标签:
【中文标题】无法在 android 的 AlramManager 中设置指定日期【英文标题】:Can't set a specified date in AlramManager in android 【发布时间】:2012-05-26 12:46:06 【问题描述】:我有以下代码为AlaramManager
设置date
,
AlarmManager alarm = (AlarmManager)getSystemService(Service.ALARM_SERVICE);
int mYear = 2012;
int mMonth = 5;
int mDay = 26;
int mHour = 12;
int mMinute = 34;
Date dateNotif = new Date (mYear - 1900, mMonth, mDay, mHour, mMinute);
Long myDate = dateNotif.getTime();
Long current = System.currentTimeMillis();
Intent i = new Intent(getApplicationContext(),ShowNotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 12345,i, 0);
alarm.set(AlarmManager.RTC_WAKEUP, dateNotif.getTime(), pendingIntent);
当模拟器时间为12:34
时,通知不会出现
【问题讨论】:
试试this答案 【参考方案1】:我已经设置了今天的日期和时间
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);//2012
calendar.set(Calendar.MONTH, 4);// It start from 0-11 so use 4 instead of 5
calendar.set(Calendar.DAY_OF_MONTH, 26);
calendar.set(Calendar.HOUR_OF_DAY,9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent i = new Intent(getApplicationContext(),ShowNotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 12345,i, 0);
alarm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pendingIntent);
【讨论】:
以上是关于无法在 android 的 AlramManager 中设置指定日期的主要内容,如果未能解决你的问题,请参考以下文章
无法在 android (Android studio) 中测试受保护的方法
无法在 Android Studio 中安装 Android SDK