0
我有存儲在數據庫中的字符串時間值。我可以使用下面的代碼訪問這些代碼,這將返回並記錄所有時間值。如何通過Alarm Manager設置多個通知?
我需要發送通知給用戶這些單獨的時間值,但我沒有成功。它似乎只向我發送了我輸入的最後一個條目的通知,爲什麼?
廣播接收機具有onRecieve方法
@Override
public void onReceive(Context context, Intent intent)
{
helper = new TaskDBHelper(context);
SQLiteDatabase sqlDB = helper.getReadableDatabase();
Cursor cursor = sqlDB.query(TaskContract.TABLE,
new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK, TaskContract.Columns.DATE},
null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Name = cursor.getString(1);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_cast_light)
.setContentTitle("Please Take " + Name)
.setContentInfo("Time")
.setContentText("Enjoy")
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setVibrate(new long[]{500, 500, 500, 500}) //set virbrate & color of led
.setLights(Color.BLUE, 3000, 3000);
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(getID(), builder.build());
cursor.moveToNext();
}
}
方法查找和循環穿過分貝爲時間的值。
private void setUpNot(){
helper = new TaskDBHelper(Meds2.this);
SQLiteDatabase sqlDB = helper.getReadableDatabase();
Cursor cursor = sqlDB.query(TaskContract.TABLE,
new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK, TaskContract.Columns.DATE},
null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) { // Loop until all vales have been seen
String time = cursor.getString(2);
String[] parts = time.split(":"); //Split String Value stored in db
String part1 = parts[0]; // hour
String part2 = parts[1]; // minute
int hr = Integer.parseInt(part1);
int min = Integer.parseInt(part2);
final int _id=(int)System.currentTimeMillis();
Intent alarmIntent = new Intent(Meds2.this, AlarmReceiver.class); //set up alarm
pendingIntent = PendingIntent.getBroadcast(Meds2.this, _id, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hr);
calendar.set(Calendar.MINUTE, min); //set cal time based of db value
/* Repeating on every 24 hours interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent); // run every date.time() in millisec
Log.e(TAG, "DATE VALUE TIME IS " + part1 +"--" +part2); //log part one and two to make sure time is right
cursor.moveToNext();
}
}
的getId()
public static int getID() {
AtomicInteger c = new AtomicInteger(0);
return c.incrementAndGet();
}
您是否在pendingIntent = PendingIntent.getBroadcast(Meds2.this,_id,alarmIntent,0)中每次檢查_id的值?相同還是不同? –
@ShadabAnsari我只是做了謝謝,yep數字通過每個循環改變。 – Rueben
什麼是nManager.notify(getID(),builder.build())中的getID() –