我正在根据用户的时间显示通知,当我启动应用时,它始终显示通知,即使在取消后也会一直重复,这是不正确的
我没有在.setWhen
上设置按时显示通知MainActivity.java
calendar=Calendar.getInstance();
day=calendar.get(Calendar.DAY_OF_MONTH);
month=(calendar.get(Calendar.MONTH));
year=calendar.get(Calendar.YEAR);
cLoader=new CursorLoader(this, BirthdayProvider.CONTENT_URI, null, BirthdayProvider.EVENT_DATE+"='"+day+"' and "+BirthdayProvider.EVENT_MONTH+"='"+month+"'", null, null);
c=cLoader.loadInBackground();
c.moveToFirst();
while (c.moveToNext()) {
calendar.set(Calendar.MONTH, (month-1));
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY,19);//set the alarm time
calendar.set(Calendar.MINUTE, 06);
calendar.set(Calendar.SECOND,0);
long calMillis=calendar.getTimeInMillis();
int systemTimeID=(int)System.currentTimeMillis();
Intent myIntent = new Intent(getBaseContext(), MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, systemTimeID, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calMillis, pendingIntent);
MyReceiver.java
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context.getApplicationContext(), "MyReceiver started", Toast.LENGTH_SHORT).show();
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
MyAlarmService.java
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
calendar=Calendar.getInstance();
date=calendar.get(Calendar.DAY_OF_MONTH);
month=(calendar.get(Calendar.MONTH)+1);
year=calendar.get(Calendar.YEAR);
cLoader=new CursorLoader(this, BirthdayProvider.CONTENT_URI, null, BirthdayProvider.EVENT_DATE+"='"+date+"' and "+BirthdayProvider.EVENT_MONTH+"='"+month+"'", null, null);
c=cLoader.loadInBackground();
Toast.makeText(getBaseContext(), "My service started ", Toast.LENGTH_SHORT).show();
}
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
/*int count=intent.getIntExtra("count", 0);
nameArray=intent.getStringArrayListExtra("name");
numberArray=intent.getStringArrayListExtra("number");*/
while (c.moveToNext()) {
calendar.set(Calendar.MONTH,(month-1) );
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_MONTH, date);
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 19);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
count=count+1;
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),SearchListActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent1.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RemoteViews remoteView=new RemoteViews(getPackageName(), R.layout.normal_status_bar);
remoteView.setTextViewText(R.id.title_name, name);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentIntent(pendingNotificationIntent)
.setContent(remoteView)
.setSmallIcon(R.drawable.app_icon)
.setLargeIcon(bitmapImage)
.setTicker("Today's Events")
.setWhen(calendar.getTimeInMillis())
.setAutoCancel(true)
.setContentTitle(name)
.setVibrate(new long[] {0, 200, 200, 600, 600})
.setNumber(count)
.setSound(uri)
.setLights(Color.BLUE, 1000, 1000)
.setContentText("[ Event type : "+eventype+" ]\n\n"+"[ Turns : "+turns+" ]");
Notification notification = builder.build();
mManager.notify(count, notification);
}
}
答案 0 :(得分:1)
问题在于month value set
。不要减去它calendar.get(Calendar.MONTH)
将给出正确的月份索引。
while (c.moveToNext()) {
calendar.set(Calendar.MONTH,(month) );
所以当你minus
成为previous month
时(不是你期望的当月)。月值从0
开始。这就是启动应用程序时立即触发警报的原因(因为警报已经过去)
正如评论中所说 - setWhen
只是一个时间戳,在通知发生时显示。这并不意味着需要在