警报响起后如何终止调用setAlarm。

时间:2015-02-20 17:30:54

标签: java android service alarmmanager

我的问题:setAlarms()一旦警报开始响铃,就会service一次又一次地调用<{strong> 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); }

@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;
}

&#39; TRService.class&#39;

{{1}}

1 个答案:

答案 0 :(得分:1)

删除行

TRAlarmManager.setAlarms(this);
来自onStartCommand() TRService方法的

。我相信这是导致问题的原因。