当我在从DatePicker和TimePicker中选择的特定日期和时间设置通知时,通知会在正确的时间显示,但在我取消后,它会在一段时间后重新出现。
我尝试了setAutoCancel(true)
和Notification.FLAG_AUTO_CANCEL
,但似乎没有效果。
这是我设置闹钟的代码片段
if(item.getTitle().equals("Add Reminder"))
{
Log.d("REMINDER", "Show Time Picker Entered from Decided Case");
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.date_time_layout);
dialog.setTitle("Choose Date and Time");
dialog.show();
Button ok = (Button) dialog.findViewById(R.id.alarm_set);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DatePicker dp = (DatePicker) dialog.findViewById(R.id.datePicker1);
TimePicker tp = (TimePicker)dialog.findViewById(R.id.timePicker1);
Calendar cal = Calendar.getInstance(); //for using this you need to import java.util.Calendar;
// add minutes to the calendar object
cal.set(Calendar.MONTH, dp.getMonth());
cal.set(Calendar.YEAR, dp.getYear());
cal.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
cal.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
cal.set(Calendar.MINUTE, tp.getCurrentMinute());
//cal.add(Calendar.MINUTE, 1);
//cal.setTimeInMillis(System.currentTimeMillis());
// cal.add(Calendar.SECOND, 10);
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0,
alarmintent,0);
int HELLO_ID=1;
// Get the AlarmManager service
HELLO_ID++;
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
Log.d("Alarm Set","day = "+dp.getDayOfMonth()+" month = "+(dp.getMonth()+1)+" year = "+dp.getYear()+" hours "+tp.getCurrentHour()+" minutes = "+tp.getCurrentMinute());
dialog.dismiss();
}
});
}
AlarmReciever.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// When our Alaram time is triggered , this method will be excuted (onReceive)
// We're invoking a service in this method which shows Notification to the User
Intent myIntent = new Intent(context, NotificationService.class);
context.startService(myIntent);
}
};
NotificationService.java
public class NotificationService extends Service {
private NotificationManager mManager;
Notification notification;
private static int NOTIFICATION_ID = 1;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// Getting Notification Service
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
/*
* When the user taps the notification we have to show the Home Screen
* of our App, this job can be done with the help of the following
* Intent.
*/
Intent intent1 = new Intent(this.getApplicationContext(), DecidedCase.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(),NOTIFICATION_ID, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
//notification = new Notification(R.drawable.ic_launcher,
// "Trying out Alarm", System.currentTimeMillis());
notification = new NotificationCompat.Builder(this.getApplicationContext())
.setContentTitle("ScheduLAWyer")
.setContentText("You've got a notification")
.setContentIntent(pendingNotificationIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setPriority(8)
.setTicker("A case has to be viewed").build();
//.addAction(R.drawable.ic_launcher,"Launch app",pendingNotificationIntent)
//.addAction(R.drawable.ic_launcher, "Cancel",pendingNotificationIntent).build();
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
//PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
// this.getApplicationContext(), 0, intent1,
//PendingIntent.FLAG_UPDATE_CURRENT);
/*notification.setLatestEventInfo(this.getApplicationContext(),
"sudduLAWyer", "Trying out Alarm Manager",
pendingNotificationIntent);*/
//notification.flags = Notification.FLAG_INSISTENT;
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_INSISTENT | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
//notification.defaults |= Notification.DEFAULT_VIBRATE;
//notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
mManager.notify(NOTIFICATION_ID++, notification);
Log.d("NOTIFICATION","Notficiation Set");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mManager.cancel(0);
}
}
答案 0 :(得分:2)
可能是因为闹钟。如果您致电Notification
,setAutoCancel(true)
本身无法再添加一次。
你可以尝试
{p>a.cancel(sender)
onReceive
的{{1}}你的AlarmReceiver
。
您必须重新创建该发件人对象才能取消它。
答案 1 :(得分:0)
理想情况下,您不应该使用服务来发送notification
,我认为如果您编写一些Util
函数并将service
onStart
的代码移到Util方法。
如果您的要求仅需要服务,我建议您使用IntentServive