gpt4 book ai didi

Android通知多次

转载 作者:搜寻专家 更新时间:2023-11-01 08:56:11 24 4
gpt4 key购买 nike

我有提醒用户服药的通知。当我添加新的服药时间时,例如晚上 9:00 和 9:01,只有最后一个通知处于 Activity 状态。这是我的代码:

AddTime.java:(部分)

Cursor cursor2 = sqdb.rawQuery("SELECT Name FROM Medicine WHERE _id = " + mId, null);
while(cursor2.moveToNext()) {
med = cursor2.getString(cursor2.getColumnIndex("Name"));
} //med - Name of medicine

String sql = "SELECT _id FROM Time WHERE mId = "+mId+" AND Year = "+year+" AND Month = '"+_month+"' AND Day = "+_numDay+" AND Week = '"+_week+"' AND Hour = "+_hour+" AND Minute = "+_min+" AND AMORPM = '"+_ampm+"' AND CalendarDate = '"+calDate+"'";
Cursor cursor = sqdb.rawQuery(sql, null);
int tId = 0;
while(cursor.moveToNext()) {
tId = cursor.getInt(cursor.getColumnIndex("_id"));
} //Unique id for notification

AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent _intent = new Intent(this, TimeAlarm.class);
_intent.putExtra("TIMEID", tId);
_intent.putExtra("MEDNAME", med);
Calendar c = Calendar.getInstance();
int Years = c.get(Calendar.YEAR);
int Months = c.get(Calendar.MONTH);
int Days = c.get(Calendar.DAY_OF_MONTH);
int Hours = c.get(Calendar.HOUR_OF_DAY);
int Minutes = c.get(Calendar.MINUTE);

GregorianCalendar curDate = new GregorianCalendar(Years, Months, Days, Hours, Minutes, 0);
GregorianCalendar nextDate = new GregorianCalendar(year, Month, _numDay, _hour, _min, 0);

long millisecDiff = nextDate.getTimeInMillis() - curDate.getTimeInMillis();

PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, _intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisecDiff, pIntent);

TimeAlarm.java

public class TimeAlarm extends BroadcastReceiver {

NotificationManager nm;


@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int timeid = intent.getExtras().getInt("TIMEID");
String medname = intent.getExtras().getString("MEDNAME");
CharSequence from = "Title";
CharSequence message = "Message";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
"Text", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(timeid, notif);
}
}

最佳答案

PendingIntent.getBroadcast 中的第二个参数称为请求代码,对于每个新警报都必须是唯一的,否则它将被覆盖。它在文档中说当前未使用此 requestCode。文档有误。

你有

PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, _intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisecDiff, pIntent);

你想要的是

PendingIntent pIntent = PendingIntent.getBroadcast(this, SomeUniqueIndex, _intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisecDiff, pIntent);

关于Android通知多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18750755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com