我正在創建一個應用程序,用戶可以在該應用程序中安排特定日期的預約。進行測試,當我通過設置5或10分鐘的警報進行檢查時,它會正常工作。但是,如果我設置爲數小時或數天再報警或關閉我的應用程序,然後它是不會放棄的通知.. 下面是一個方法我爲創建通知當我關閉我的應用程序時,通知無法正常工作
public void setAlarm() {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, ihour);
c.set(Calendar.MINUTE, iminute);
c.set(Calendar.DAY_OF_MONTH, idate);
c.set(Calendar.MONTH, imonth);
c.set(Calendar.YEAR, iyear);
SharedPreferences sharedPreferences = getSharedPreferences(variable.APPOINTMENT_NO, MODE_PRIVATE);
SharedPreferences.Editor editor = getSharedPreferences(variable.APPOINTMENT_NO, MODE_PRIVATE).edit();
request_id = sharedPreferences.getInt(variable.APPOINTMENT_NO, 0);
if (request_id == 0) {
request_id = 1;
editor.putInt(variable.APPOINTMENT_NO, request_id).commit();
} else {
request_id++;
editor.putInt(variable.APPOINTMENT_NO, request_id).commit();
}
Intent intent1 = new Intent(Doctor_Appointment_Set.this, AppointmentSetReciever_inner.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Doctor_Appointment_Set.this, request_id, intent1,
0);
AlarmManager am = (AlarmManager) Doctor_Appointment_Set.this
.getSystemService(Doctor_Appointment_Set.this.ALARM_SERVICE);
am.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
Log.d(getClass().getSimpleName(), request_id + "");
}
,這是我的廣播接收器類。我創建它作爲一個內部類
public static class AppointmentSetReciever_inner extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Random randomGenerator = new Random();
int index = randomGenerator.nextInt(3);
Log.d(getClass().getSimpleName(), request_id + "");
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, Show_Appointment_Notification.class);
notificationIntent.putExtra("NotificationMessage", dr_name + " | " + st_time + " | " + st_date);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, request_id,
notificationIntent, 0);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context)
.setContentTitle("Appointment")
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.male))
.setSmallIcon(R.mipmap.logo)
.setContentText("Today You Have an Appointment with Dr. " + dr_name)
//.setStyle(new NotificationCompat.BigTextStyle().bigText(tip.get(index)))
.setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setPriority(Notification.PRIORITY_MAX);
notificationManager.notify(request_id, mNotifyBuilder.build());
}
}
打印已設置新的時間和你如何在什麼操作系統版本測試 –
此外Android清單 –
請詳細...你在跑? – liorlis