Android通知多次 [英] Android notifications multiple times
问题描述
我有提醒用户吃药通知。当我服用的药物,例如,下午9:00和下午9时01分增加新的时代,只有最后的通知有效。
这里是我的code:
AddTime.java:(部分)
光标光标2 = sqdb.rawQuery(选择的名字从医学WHERE _id =+ MID,NULL);
而(cursor2.moveToNext()){
MED = cursor2.getString(cursor2.getColumnIndex(姓名));
} //梅德 - 名药 字符串的sql =SELECT _id不时WHERE MID =+ MID +和年份=+一年+和月='+ _month +'AND天=+ _numDay +和周='+ _week +'和小时= + _hour +和分钟=+ _min +和AM或PM ='+ _ampm +'AND CalendarDate ='+ calDate +';
光标光标= sqdb.rawQuery(SQL,NULL);
INT TID = 0;
而(cursor.moveToNext()){
TID = cursor.getInt(cursor.getColumnIndex(_ ID));
} //通知独特的ID AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
意图_intent =新意图(这一点,TimeAlarm.class);
_intent.putExtra(TIMEID,TID);
_intent.putExtra(MEDNAME,MED);
日历C = Calendar.getInstance();
INT年= c.get(Calendar.YEAR);
INT个月= c.get(的Calendar.MONTH);
INT天= c.get(Calendar.DAY_OF_MONTH);
INT小时= c.get(Calendar.HOUR_OF_DAY);
INT分钟= c.get(Calendar.MINUTE); GregorianCalendar的CURDATE =新的GregorianCalendar(年,月,日,时,分,0);
GregorianCalendar的nextDate =新的GregorianCalendar(年,月,_numDay,_hour,_min,0); 长millisecDiff = nextDate.getTimeInMillis() - curDate.getTimeInMillis(); 的PendingIntent pIntent = PendingIntent.getBroadcast(这一点,0,_intent,PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()+ millisecDiff,pIntent);
TimeAlarm.java
公共类TimeAlarm扩展广播接收器{ NotificationManager纳米;
@燮pressWarnings(德precation)
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
纳米=(NotificationManager)上下文
.getSystemService(Context.NOTIFICATION_SERVICE);
INT TIMEID = intent.getExtras()调用getInt(TIMEID);
串medname = intent.getExtras()的getString(MEDNAME);
从CharSequence的=标题;
CharSequence的消息=消息;
的PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,
新意图(),0);
注意notif =新的通知(R.drawable.icon,
文本,System.currentTimeMillis的());
notif.setLatestEventInfo(背景下,从日消息,contentIntent);
nm.notify(TIMEID,notif);
}
}
在您的PendingIntent.getBroadcast称为请求code中的第二个参数需要为每一个新的报警唯一的,否则会覆盖。它说,这一请求code当前未使用的文件中。该文档是错误的。
您
的PendingIntent pIntent = PendingIntent.getBroadcast(这一点,0,_intent,PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()+ millisecDiff,pIntent);
您需要的是
的PendingIntent pIntent = PendingIntent.getBroadcast(这一点,SomeUniqueIndex,_intent,PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()+ millisecDiff,pIntent);
I have notifications that remind user to take the medicine. When I add new times for taking medicines, for example, at 9:00 pm and 9:01 pm, only last notification is active. Here is my code:
AddTime.java: (part)
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);
}
}
The second parameter in your PendingIntent.getBroadcast known as the request code needs to be unique for each new alarm, otherwise it will overwrite. It says in the documentation that this requestCode is not currently used. The documentation is wrong.
you have
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, _intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisecDiff, pIntent);
What you want is
PendingIntent pIntent = PendingIntent.getBroadcast(this, SomeUniqueIndex, _intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisecDiff, pIntent);
这篇关于Android通知多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!