Android Alarm Manager - 随机时间触发警报

时间:2017-09-21 05:03:15

标签: android alarmmanager

我是android中的新手,我目前正在使用调度程序应用程序。这是我使用SQLite数据库值设置警报的代码。 我的问题是警报是随机时间触发的。

public void startAlarm()抛出ParseException {

    Date dt = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
    String time = sdf.format(dt);
    String start_time;

    Calendar sCalendar = Calendar.getInstance();
    String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());

    DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
    SQLiteDatabase sqLiteDatabase =  helper.getReadableDatabase();
    String checktime = "Select * from SCHEDULE where week_day='"+dayLongName+"'";

    Cursor cursor = sqLiteDatabase.rawQuery(checktime, null);
        if(cursor.moveToNext()) {

            start_time = cursor.getString(cursor.getColumnIndex(DatabaseHelper.STARTTIME));

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a",Locale.getDefault());
            SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("hh:mm",Locale.getDefault());

            Date milli =simpleDateFormat2.parse(start_time);
            String milli2 = simpleDateFormat2.format(milli);

            Date timeparsed1 = simpleDateFormat.parse(start_time);
            String timeparsed2 = simpleDateFormat.format(timeparsed1);

              String s = milli2;
              Pattern p = Pattern.compile("(\\d+):(\\d+)");
              Matcher m = p.matcher(s);

              if (m.matches()) {
                  int hrs = Integer.parseInt(m.group(1));
                  int min = Integer.parseInt(m.group(2));

                  long ms = (long) hrs * 60 * 60 * 1000 + min * 60 * 1000;
            //      System.out.println("hrs=" + hrs + " min=" + min + " ms=" + ms);

                  Intent intent = new Intent(this, MyBroadcastReceiver.class);

                     PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);

                  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

                  alarmManager.set(AlarmManager.RTC_WAKEUP, ms , pendingIntent);

               //Toast.makeText(getApplicationContext(),String.valueOf(ms),Toast.LENGTH_SHORT).show();

              } else {

                  Toast.makeText(getApplicationContext(),"Bad Format",Toast.LENGTH_SHORT).show();

              }
}

2 个答案:

答案 0 :(得分:0)

在Android 4.4之后,谷歌推出了操作系统电源管理机制,以最大限度地减少用电量。尝试在代码中添加类似于下面的版本检查。如果版本是KitKat及更高版本,请使用setExact()。否则使用set()。

见下面的例子:

        //For Android version 4.4 up, use setExact() for the alarm
        //otherwise use set()
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncInterval * 1000, pendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncInterval * 1000, pendingIntent);
        }

Documentation reference here

答案 1 :(得分:0)

尝试这套代码,您的服务永远不会阻止它适合我。 " setInexactReapeating"许多棒棒糖和棉花糖设备一旦启动就无法工作,然后失去对服务的控制,这将有助于你。

if (android.os.Build.VERSION.SDK_INT >= 23)
{
    piLR = PendingIntent.getBroadcast(context, 0, intentLR,
                PendingIntent.FLAG_UPDATE_CURRENT);
    amLR.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                interval, piLR);
}
else if (android.os.Build.VERSION.SDK_INT >= 19
            && android.os.Build.VERSION.SDK_INT < 23)
{
    piLR = PendingIntent.getBroadcast(context, 0, intentLR,
                PendingIntent.FLAG_UPDATE_CURRENT);
    amLR.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), interval, piLR);
}
else
{
    amLR.setRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), interval, piLR);
}