gpt4 book ai didi

java - 闹钟响起后如何终止调用 setAlarm。

转载 作者:行者123 更新时间:2023-12-01 11:53:06 24 4
gpt4 key购买 nike

我的问题: 一旦闹钟启动,service 就会一次又一次地调用 setAlarms() 方法(每秒 64 次!!)铃声。闹钟响后如何停止调用?

我的代码

TRTimeReminder.class

这是可以设置新闹钟的类。

TRAlarmManager.cancelAlarms(this);  //cancel the the alarm is it's already set

//I have set if..else here
trDb.updateReminder(Value, title, des, date, time); //update the database row if alarm's updated
trDb.addReminder(title, des, date, time); //otherwise add new entry in database

TRAlarmManager.setAlarms(this); //set the alarm
setResult(RESULT_OK);

TRAlarmManager.class

public static void setAlarms(Context context) {
TRDBHelper trDb = new TRDBHelper(context);
List<TRListFormat> list = trDb.getAllReminders();

for(TRListFormat reminder : list) {
Calendar calendar;
Date date;

PendingIntent pIntent = createPendingIntent(context, reminder);

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");

try{
date = sdf.parse(reminder.getDate() + " " + reminder.getTime());
calendar = Calendar.getInstance();
calendar.setTime(date);

setAlarm(context, calendar, pIntent);

Log.d("parsing date", "date parsed");
}catch(ParseException e){
e.printStackTrace();
Log.d("cannot parse", "failure");
}
trDb.close();
}
}

@SuppressLint("NewApi")
public static void setAlarm(Context context, Calendar calendar, PendingIntent pIntent){

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);

Log.d("set alarm called", "null");
}
else {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
Log.d("set alarm called", "null");
}
}

public static void cancelAlarms(Context context) {
TRDBHelper trDb = new TRDBHelper(context);
List<TRListFormat> list = trDb.getAllReminders();

if(list != null){
for(TRListFormat reminder : list){
PendingIntent pIntent = createPendingIntent(context, reminder);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
}
}
}

private static PendingIntent createPendingIntent(Context context, TRListFormat format) {

Intent intent = new Intent(context, TRService.class);
intent.putExtra("id", format.getId());
intent.putExtra("title", format.getTitle());
intent.putExtra("des", format.getDes());
intent.putExtra("date", format.getDate());
intent.putExtra("time", format.getTime());

return PendingIntent.getService(context, format.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

'TRService.class'

@Override
public int onStartCommand(Intent intent, int flags, int startId){

Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_SHORT).show();

Intent alarmIntent = new Intent(getBaseContext(), TRAlarmScreen.class);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmIntent.putExtras(intent);
getApplication().startActivity(alarmIntent);

TRAlarmManager.setAlarms(this);

return START_STICKY;
}

最佳答案

删除该行

TRAlarmManager.setAlarms(this);

来自TRServiceonStartCommand()方法。我相信这就是导致问题的原因。

关于java - 闹钟响起后如何终止调用 setAlarm。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28634848/

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