我只是编辑我之前的问题(android模式锁定)。 谢谢你的帮助.. 现在我想知道如何取消闹钟。
protected void setReminder(Date reminder_date){ // TODO自动生成的方法存根
String al_date = sdf.format(reminder_date);
Log.d("alarm_date", al_date);
int r_day, r_month, r_year;
Calendar ca = Calendar.getInstance();
ca.setTime(reminder_date);
r_day = ca.get(Calendar.DAY_OF_MONTH);
r_month = ca.get(Calendar.MONTH);
r_year = ca.get(Calendar.YEAR);
Log.d("new month", String.valueOf(r_month));
Calendar calendar = Calendar.getInstance(TimeZone.getDefault(),
Locale.getDefault());
calendar.set(Calendar.MONTH, r_month);
calendar.set(Calendar.YEAR, r_year);
calendar.set(Calendar.DAY_OF_MONTH, r_day);
calendar.set(Calendar.HOUR_OF_DAY, 10); // HOUR
calendar.set(Calendar.MINUTE, 15); // MIN
calendar.set(Calendar.SECOND, 0); // SEC
calendar.set(Calendar.AM_PM, Calendar.AM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,
myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int level=getAPILevel();
if(level == android.os.Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
}
if(level < android.os.Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
}
Log.d("alarm","on");
}
这是我设置闹钟的方式。这工作正常......但问题是即使过期时也会发送通知。
这是我的接收器类。 公共类MyReceiver扩展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
MyAlarmService.class
@SuppressWarnings({&#34; static-access&#34;,&#34; deprecation&#34;}) @覆盖 public void onStart(Intent intent,int startId) { super.onStart(intent,startId);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),HomePage.class);
Notification notification = new Notification(R.drawable.ic_launcher,"Time for payment!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "Finance Manager", "Time for payment!", pendingNotificationIntent);
Log.d("Notification","send");
mManager.notify(0, notification);
}