Android 安卓报警管理器-随机时间触发报警

Android 安卓报警管理器-随机时间触发报警,android,alarmmanager,Android,Alarmmanager,我是android中的新手,目前正在开发调度程序应用程序。这是我使用SQLite db值设置报警的代码。 我这里的问题是,警报是在随机时间触发的 public void startAlarm()引发异常{ Date dt = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a"); String time = sdf.format(dt); String start_time;

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

public void startAlarm()引发异常{

    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();

              }
}

继安卓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);
        }

继安卓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);
        }

试试这套代码你的服务永远不会停止它对我有效。“SetInExactreRepeating”在许多棒棒糖和棉花糖设备中都不起作用一旦启动,就会失去对服务的控制这将帮助你解决问题

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);
}
if(android.os.Build.VERSION.SDK\u INT>=23)
{
piLR=PendingEvent.getBroadcast(上下文,0,意图,
PendingEvent.FLAG_UPDATE_CURRENT);
amLR.setAndAllowWhileIdle(AlarmManager.EASSED\u REALTIME\u唤醒,
间隔(piLR);
}
else if(android.os.Build.VERSION.SDK_INT>=19
&&android.os.Build.VERSION.SDK_INT<23)
{
piLR=PendingEvent.getBroadcast(上下文,0,意图,
PendingEvent.FLAG_UPDATE_CURRENT);
amLR.setInexactRepeating(AlarmManager.RTC_唤醒,
System.currentTimeMillis(),间隔,piLR);
}
其他的
{
amLR.setRepeating(AlarmManager.RTC_唤醒,
System.currentTimeMillis(),间隔,piLR);
}

试试这套代码你的服务永远不会停止它对我有效。“SetInExactreRepeating”在许多棒棒糖和棉花糖设备中都不起作用一旦启动,就会失去对服务的控制这将帮助你解决问题

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);
}
if(android.os.Build.VERSION.SDK\u INT>=23)
{
piLR=PendingEvent.getBroadcast(上下文,0,意图,
PendingEvent.FLAG_UPDATE_CURRENT);
amLR.setAndAllowWhileIdle(AlarmManager.EASSED\u REALTIME\u唤醒,
间隔(piLR);
}
else if(android.os.Build.VERSION.SDK_INT>=19
&&android.os.Build.VERSION.SDK_INT<23)
{
piLR=PendingEvent.getBroadcast(上下文,0,意图,
PendingEvent.FLAG_UPDATE_CURRENT);
amLR.setInexactRepeating(AlarmManager.RTC_唤醒,
System.currentTimeMillis(),间隔,piLR);
}
其他的
{
amLR.setRepeating(AlarmManager.RTC_唤醒,
System.currentTimeMillis(),间隔,piLR);
}

同样,运行应用程序后,警报会在3秒钟后触发。3秒钟后是否一致?是的,但我不确定这3秒钟是否只是一个估计值。运行应用程序后,警报会在3秒钟后触发。3秒钟后是否一致?是的,但我不确定这3秒钟是否只是一个估计值。你可以吗