gpt4 book ai didi

安卓闹钟未设置

转载 作者:行者123 更新时间:2023-11-29 19:03:18 24 4
gpt4 key购买 nike

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

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)
}

IntentService类如下:

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
}

}

服务在 list 中声明为:

<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 天的测试 session ,我可能缺少哪些基础知识?

最佳答案

抱歉,因为我忙于另一项任务,所以没有仔细阅读您的所有代码,所以这是我与 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);}

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

关于安卓闹钟未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48080609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com