简体   繁体   English

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

[英]Android Alarm Manager - alarm firing on random time

Im a just new in android and im currently working on scheduler app.This is my code on setting alarm using SQLite db values. 我是android中的一个新手,目前正在调度程序应用程序上工作。这是我使用SQLite db值设置警报的代码。 My problem here is that the alarm is triggering on random time. 我的问题是警报是在随机时间触发的。

public void startAlarm() throws ParseException { 公共无效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();

              }
}

After Android 4.4, Google has introduced OS power management mechanism to minimize power use. 在Android 4.4之后,Google引入了操作系统电源管理机制以最大程度地减少功耗。 Try adding a version checking in your code similar to the one below. 尝试在代码中添加一个版本检查,类似于下面的代码。 If the version is KitKat and above, use setExact(). 如果版本为KitKat或更高版本,请使用setExact()。 Otherwise use set(). 否则使用set()。

See example below: 请参见下面的示例:

        //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 文档参考在这里

Try this set of code your service never going to stop it works for me. 试试这组代码,您的服务将永远不会停止对我有用。 "setInexactReapeating" not working in many of the lollipop and marshmallows devices once it started then lose control over the service this will help you out. 一旦启动,“ 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM