通知开始从AlarmManager不断重复再 [英] Notification started from AlarmManager keeps repeating again
问题描述
当我设置的通知从一个datepicker和TimePicker选择特定的日期和时间,该通知出现在正确的时间,但之后,我取消它,它一段时间后再次出现。结果
我曾尝试 setAutoCancel(真)
和 Notification.FLAG_AUTO_CANCEL
,但他们似乎没有工作。
这里是code-片段在那里我设置报警
如果(item.getTitle()。等于(添加提醒))
{
Log.d(提醒,显示时间选择器从决定该案进入);
最后对话的对话=新的对话框(本); dialog.setContentView(R.layout.date_time_layout); dialog.setTitle(选择日期和时间); dialog.show();
OK按钮=(按钮)dialog.findViewById(R.id.alarm_set);
ok.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
DatePicker的DP =(的DatePicker)dialog.findViewById(R.id.datePicker1);
TimePicker TP =(TimePicker)dialog.findViewById(R.id.timePicker1); 日历CAL = Calendar.getInstance(); //使用这个,你需要导入java.util.Calendar中; //分钟添加到日历对象
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);
意图alarmintent =新意图(getApplicationContext(),AlarmReceiver.class);
发件人的PendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0,
alarmintent,0);
INT HELLO_ID = 1;
//获取AlarmManager服务
HELLO_ID ++;
AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),发送者);
Log.d(报警集,天=+ dp.getDayOfMonth()+一个月=+(dp.getMonth()+ 1)+年=+ dp.getYear()+小时+ tp.getCurrentHour()+分钟=+ tp.getCurrentMinute());
dialog.dismiss(); }
});
}
AlarmReciever.java
公共类AlarmReceiver扩展广播接收器{ @覆盖
公共无效的onReceive(上下文的背景下,意图意图){ //当我们的Alaram时间被触发,该方法将被excuted(的onReceive)
//我们调用这个方法的服务,显示通知用户
意图myIntent =新意图(背景下,NotificationService.class);
context.startService(myIntent);}
};
NotificationService.java
公共类NotificationService延伸服务{私人NotificationManager mManager的;
通知通知;
私有静态诠释NOTIFICATION_ID = 1;@覆盖
公众的IBinder onBind(意向为arg0){
// TODO自动生成方法存根
返回null;
}@覆盖
公共无效的onCreate(){
super.onCreate();
}@覆盖
公共无效调用onStart(意向意图,诠释startId){
super.onStart(意向,startId);
//获取通知服务
mManager的=(NotificationManager)this.getApplicationContext()
.getSystemService(
this.getApplicationContext()NOTIFICATION_SERVICE)。
/ *
*当用户点击该通知,我们必须显示主屏幕
*我们的应用程序,这工作可以用下面的帮助下完成
*意图。
* /
意图intent1 =新意图(this.getApplicationContext(),DecidedCase.class);
的PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(),NOTIFICATION_ID,intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
//通知=新的通知(R.drawable.ic_launcher,
//试行报警,System.currentTimeMillis的());
通知=新NotificationCompat.Builder(this.getApplicationContext())
.setContentTitle(ScheduLAWyer)
.setContentText(你的通知)
.setContentIntent(pendingNotificationIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(真)
.setPriority(8)
。.setTicker(一个案例来看待)建设();
//.addAction(R.drawable.ic_launcher,\"Launch应用,pendingNotificationIntent)
//.addAction(R.drawable.ic_launcher,取消,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,试行报警管理器中,
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 ++,通知);
Log.d(通知,Notficiation集);
}@覆盖
公共无效的onDestroy(){
// TODO自动生成方法存根
super.onDestroy();
mManager.cancel(0);
}}
这可能是因为报警。 通知
如果调用本身不能添加一个更多的时间 setAutoCancel(真)
。
您可以尝试
a.cancel(发件人)
AlarmReceiver $ C $在
的onReceive
C>你的事。
你将不得不重新创建sender对象取消它。
When I set a Notification on a particular date and time chosen from a DatePicker and a TimePicker, The notification appears on the correct time but after I cancel it, it reappears after some time.
I have tried setAutoCancel(true)
and Notification.FLAG_AUTO_CANCEL
but they don't seem to work.
here is the code-snippet where I'm setting alarm
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);
}
}
It is probably because of the alarm. Notification
itself cannot add it one more time if you call setAutoCancel(true)
.
You can try
a.cancel(sender)
in onReceive
of AlarmReceiver
of yours.
You will have to recreate that sender object to cancel it.
这篇关于通知开始从AlarmManager不断重复再的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!