我想開發一個應用程序,但我面對的一個問題創建通知... 在我的應用我有像時針和分針以及一些其他的東西一些數據的數據庫,我想創建一個通知時間列。使用SQLite
我創建了這些代碼通知:
public void scheduleNotification(Notification notification, int hour, int minute) {
Intent notificationIntent = new Intent(getContext().getApplicationContext(), TaskReceiver.class);
notificationIntent.putExtra(TaskReceiver.NOTIFICATION_ID, 0);
notificationIntent.putExtra(TaskReceiver.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext().getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(getContext());
builder.setContentTitle("Text Title");
builder.setContentText("Some Text");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setAutoCancel(true);
builder.setVibrate(new long[]{1000, 1000, 1000});
Intent intent = new Intent(getContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 1, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
builder.setContentIntent(pendingIntent);
return builder.build();
}
,並調用它像活動:
Cursor c = dbManager.fetch();
if(c != null){
while (c.moveToNext()){
int hour = c.getInt(c.getColumnIndex(DatabaseHelper.CLOCK_HOUR));
int minute = c.getInt(c.getColumnIndex(DatabaseHelper.CLOCK_MINUTE));
scheduleNotification(getNotification("Test"), hour, minute);
}
}else {
Toast.makeText(getContext(), "Cursor is empty", Toast.LENGTH_SHORT).show();
}
但它不工作,誰能幫助我?
試試這種方式https://stackoverflow.com/questions/44204387/alarmmanager-setinexactrepeating-setwindow-setrepeating-methods-do-not-fire-al/44205413#44205413 –
@AniruddhParihar它不爲我工作,因爲當我讀數據庫中的數據,並將它們設置的通知,通知不顯示。我不知道爲什麼。 – rexo
「不工作」是什麼意思?你看到錯誤信息,你有沒有嘗試過任何調試?如果是,結果如何? – PPartisan