Android 未设置闹钟

Android Alarm Not Set

我曾经在其他项目中使用以下代码段将警报设置为重复和非重复,但现在让我发疯的是愚蠢的错误可能是什么我已经为我当前的实现不发出警报 :\ :

private fun setAlarm(obj: MyObject, time: Long) {
    val intent = Intent(applicationContext, MyAlarmIntentService::class.java)
    intent.putExtra(C.KEY_ME, obj)

    val pendingIntent = PendingIntent.getService(applicationContext, 43, intent, 
                                          PendingIntent.FLAG_UPDATE_CURRENT)

    val context = this@MyActivity
    val cal = Calendar.getInstance()
    cal.timeInMillis = time * 1000   // time is in seconds
    Log.d("setAlarm", "setting time -> $time for obj = $obj")
    Log.d("setAlarm", "Set calendar: " + cal.toString())

    val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)  // Overriding doze-mode
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        val alarmClockInfo =
                AlarmManager.AlarmClockInfo(cal.timeInMillis, null)
        alarmManager.setAlarmClock(alarmClockInfo, pendingIntent)
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) // Overriding doze-mode
        // Not-working in Nexus 6 - API 25 (7.1.1), but works in API 23 (6.0.1, tested in multiple devices)
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, 
                    cal.timeInMillis, pendingIntent) 
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Works
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.timeInMillis, pendingIntent)
    else // works
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.timeInMillis, pendingIntent)
}

IntentServiceclass如下:

class MyAlarmIntentService : IntentService("Notification") {

  override fun onHandleIntent(intent: Intent?) {
    val obj = intent?.getParcelableExtra<MyObj>(C.KEY_ME) ?: return
    Lg.d(TAG, "My alarm fired for - \n" + obj.toString())
  }

  companion object {
    private val TAG = ContestAlarmIntentService::class.java.simpleName
  }

}

服务在清单中声明为:

<service android:name=".services.MyAlarmIntentService" />

下面是提到日志里面setAlarm()方法的例子:

E/setAlarm: setting time -> 1514993850 for obj = MyObj{...}
E/setAlarm: Set calendar: java.util.GregorianCalendar[time=1514993850000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=libcore.util.ZoneInfo[id="Asia/Dhaka",mRawOffset=21600000,mEarliestRawOffset=23400000,mUseDst=false,mDstSavings=0,transitions=7],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=3,DAY_OF_YEAR=3,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=37,SECOND=30,MILLISECOND=0,ZONE_OFFSET=21600000,DST_OFFSET=0]

这对我来说曾经是一个直截了当的代码,但是对于这 3 天 的测试会话,我可能缺少哪个基础知识?

抱歉,我忙于另一项任务,没有仔细阅读您的所有代码,所以这是我用于 Broadcast

的代码
Intent intent = new Intent(context, AlarmReceiver.class);
    intent.setAction(UtilAlarmConstants.ALARM_ACTION);
pIBroadCast = PendingIntent.getBroadcast(context, ALARM_PI_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);

    if (alarmManager != null) {

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
          alarmManager.setExactAndAllowWhileIdle(ALARM_TYPE, calendar.getTimeInMillis(),pendingIntent);

        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AlarmManager.AlarmClockInfo alarmClockInfo
                    = new AlarmManager.AlarmClockInfo(System.currentTimeMillis() + fireAT, null);
            alarmManager.setAlarmClock(alarmClockInfo, pIBroadCast);
        }
        //setExact for 19
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, fireAT, pIBroadCast);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, fireAT, pIBroadCast);
        }
    }

和广播

    if (intent != null && intent.getAction() != null) {

        if (intent.getAction().equalsIgnoreCase(UtilAlarmConstants.ALARM_ACTION)) {

            setUpNotification(context, intent);}

很抱歉没有给出与服务相关的答案,因为它们都是一样的古怪古怪:)