我的應用程序正在根據存儲在數據庫中的時間生成通知,就像用戶點擊通知按鈕時一樣,所有警報都會在後面設置並每天生成通知,現在的問題是,當我按通知按鈕時,即使它不是noti時間,它也會立即生成通知,我觀察到這是由於數據庫中的時間。這已經像例如一樣通過了。我在2:30:00按通知按鈕,但在我的數據庫中存儲時間爲2:28:00,因此它會生成該時間的通知。因爲我測試了設置時間,然後當前和它工作正常(就像沒有通知產生的點擊)。 那麼如何停止獲得前一次的通知?如何在特定情況下停止通知
for (int i = 0; i < C.getCount(); i++) {
h[i] = Integer.valueOf(DealTimes[i].split(":")[0]);
m[i] = Integer.valueOf(DealTimes[i].split(":")[1]);
// Toast.makeText(getBaseContext(), "Time at 16th index " + h + "min " + m, Toast.LENGTH_SHORT).show();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, h[i]);
calendar.set(Calendar.MINUTE, m[i]);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
A[i] = calendar.getTimeInMillis();
// Toast.makeText(getBaseContext(), "Final calnder Time in mili sec " + A[i] , Toast.LENGTH_SHORT).show();
// Toast.makeText(getBaseContext(), "Final calnder Only Time " + calendar.getTime() + " Time zone " + calendar.getTimeZone(), Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "Setting Alarm", Toast.LENGTH_SHORT).show();
Intent alertIntent = new Intent(this, AlertReceiver.class);
final int _id = (int) System.currentTimeMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, A[i],
PendingIntent.getBroadcast(this, _id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,Allah[i],AlarmManager.INTERVAL_DAY, PendingIntent.getBroadcast(this, _id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
這是我的廣播接收器的代碼
private static int NOTIFICATION_ID = 1;
NotificationManager notificationManager;
private PendingIntent pendingIntent;
SQLiteDB handler;
@Override
public void onReceive(Context context, Intent intent) {
String myUserid = "1"; // = new SavedPreferences(context).getUserId();
long[] blah = intent.getLongArrayExtra("id");
handler = new SQLiteDB(context);
handler.open();
Cursor C = handler.returnData();
String notificationUserId = intent.getStringExtra("NotificationUserId");
if (!TextUtils.isEmpty(myUserid) && myUserid.equals(notificationUserId)) {
String bookingId = intent.getStringExtra("bookingId");
System.out.println("SP ID IN RECEIVER"+intent.getStringExtra("spId"));
String spId = intent.getStringExtra("spId");
// change before using for real
for (int i = 0; i <C.getCount(); i++) {
if (new Date().getTime() > blah[i]) {//dbTime) {// Here time is past so need not to play notification.
} else {// Notification time is in future. So can play the notification.
notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(context, MainActivity.class);
NOTIFICATION_ID = Integer.parseInt(intent.getStringExtra("Id"));
mIntent.putExtra("message", intent.getStringExtra("message"));
mIntent.putExtra("Id", NOTIFICATION_ID);
mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(context, 111, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);
builder.setAutoCancel(true);
builder.setContentTitle("Test");
builder.setContentText(intent.getStringExtra("message"));
builder.setSmallIcon(R.drawable.cast_ic_notification_forward);
builder.setContentIntent(pendingIntent);
// builder.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.service_completed_text)));
notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
}
感謝這個答案,但你能解釋我什麼是RatingActivity.class是在你的代碼?我已經編輯了我關於yout代碼的alert recvier,在我的問題(已編輯)中提到了它。請看看它因爲它不工作,現在我甚至沒有收到任何通知,甚至在正確的時間。 –