一旦启动报警服务,两个报警都会触发

时间:2015-07-26 05:53:42

标签: android

一旦我启动AlarmService,两个警报都会触发,而不会关注格式为HH:MM的SQLite数据库中保存的时间。 我尝试了以下代码:

public class AlarmService extends Service {
    SQLiteDatabase db;

    @Override
    public void onCreate() {

        Toast.makeText(getApplicationContext(), "Alarm Service Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(getApplicationContext(), "Alarm Service Started", Toast.LENGTH_LONG).show();
        long oneDay = 24*3600*1000; //24 hours


        Intent intent1 = new Intent(getBaseContext(), StartTimeIntentReceiver.class);


        final PendingIntent sender1 = PendingIntent.getBroadcast(
                this, 192837, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intent2 = new Intent(getBaseContext(), EndTimeIntentReceiver.class);

        final PendingIntent sender2 = PendingIntent.getBroadcast(
                this, 192837, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        db = openOrCreateDatabase("EventsDB", Context.MODE_PRIVATE, null);

        Cursor c = db.rawQuery("SELECT * from TIntervals", null);
        c.moveToFirst();
        String starttime = c.getString(c.getColumnIndex("stime"));
        String endtime = c.getString(c.getColumnIndex("etime"));
        c.moveToNext();
        c.close();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        try {
            long sTime = simpleDateFormat.parse(starttime).getTime();
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.RTC_WAKEUP, sTime, oneDay, sender1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm", Locale.getDefault());
        simpleDateFormat1.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            long eTime = simpleDateFormat.parse(endtime).getTime();
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.RTC_WAKEUP, eTime, oneDay, sender2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return Service.START_STICKY;
    }

}

I also tried the following code which starts the service but never fires an alarm:




public class AlarmService extends Service {
    SQLiteDatabase db;

    @Override
    public void onCreate() {

        Toast.makeText(getApplicationContext(), "Alarm Service Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(getApplicationContext(), "Alarm Service Started", Toast.LENGTH_LONG).show();
        long oneDay = 24*3600*1000; //24 hours

        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);


        Intent intent1 = new Intent(getBaseContext(), StartTimeIntentReceiver.class);



        final PendingIntent sender1 = PendingIntent.getBroadcast(
                this, 192837, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intent2 = new Intent(getBaseContext(), EndTimeIntentReceiver.class);

        final PendingIntent sender2 = PendingIntent.getBroadcast(
                this, 192837, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        db = openOrCreateDatabase("EventsDB", Context.MODE_PRIVATE, null);

        Cursor c = db.rawQuery("SELECT * from TIntervals", null);
        c.moveToFirst();
        String starttime = c.getString(c.getColumnIndex("stime"));
        String endtime = c.getString(c.getColumnIndex("etime"));
        c.moveToNext();
        c.close();

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int hours = Integer.valueOf(starttime.split(":")[0]);
        int minutes = Integer.valueOf(starttime.split(":")[1]);
        calendar.set(Calendar.HOUR, hours);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DAY_OF_WEEK, 1);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), oneDay, sender1);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(System.currentTimeMillis());
        int hours2 = Integer.valueOf(endtime.split(":")[0]);
        int minutes2 = Integer.valueOf(endtime.split(":")[1]);
        calendar2.set(Calendar.HOUR, hours2);
        calendar2.set(Calendar.MINUTE, minutes2);
        calendar2.set(Calendar.SECOND, 0);
        calendar2.add(Calendar.DAY_OF_WEEK, 1);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), oneDay, sender2);



        return Service.START_STICKY;
    }

}

请帮助并推荐上述任何代码的任何更改。感谢。

1 个答案:

答案 0 :(得分:0)

我以这种方式设置闹钟。这对我有用

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

    Intent myIntent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
            myIntent, 0);
    Calendar firingCal = Calendar.getInstance();
    Calendar currentCal = Calendar.getInstance();

    firingCal.set(Calendar.HOUR, 8); // At the hour you wanna fire
    firingCal.set(Calendar.MINUTE, 0); // Particular minute
    firingCal.set(Calendar.SECOND, 0);// particular second
    firingCal.set(Calendar.AM_PM, Calendar.AM);//in my case I specified AM-PM

    long intendedTime = firingCal.getTimeInMillis();
    long currentTime = currentCal.getTimeInMillis();

    if (intendedTime >= currentTime) // you can add buffer time too here to
                                        // ignore some small differences in
                                        // milliseconds
    {
        // set from today
        am.set(AlarmManager.RTC_WAKEUP, intendedTime, pendingIntent);

    } else {
        firingCal.add(Calendar.DAY_OF_MONTH, 1);
        intendedTime = firingCal.getTimeInMillis();

        am.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime,
                AlarmManager.INTERVAL_DAY, pendingIntent);

    }  

此功能在每天上午8点设置闹钟。