android - 应用程序关闭时警报不响(Android Studio)
问题描述
我已经被这个问题困扰了好几天了。我想设置一个闹钟,然后在闹钟响起时发送通知。应用程序打开时运行良好,但应用程序关闭时也应该运行。
这是代码:
安卓清单:
<receiver android:name=".note.AlertReceiver"
android:enabled="true"
android:exported="true" ></receiver>
<service android:name=".note.AlarmNotificationService"
android:enabled="true"
android:exported="true" ></service>
警报接收器:
public class AlertReceiver extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
Log.d("done", "done");
Intent serviceIntent = new Intent(context,AlarmNotificationService.class);
context.startService(serviceIntent);
}
}
警报通知服务:
public class AlarmNotificationService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("done","done1");
return START_NOT_STICKY;
}
主要活动:
@RequiresApi(api = Build.VERSION_CODES.N)
private void setAlarm(Calendar calendar){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this , AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,REQUEST_CODE_alarm,intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
}
setAlarm
TimePicker
对话框关闭后触发。我不会添加时间选择器代码,因为它适用于打开的应用程序。
当应用程序关闭时。done
不会出现在 logcat 中所以我假设BroadcastReceiver
没有收到任何东西
解决方案
我没有使用AlarmManager
过,Service
因此我很难理解为什么它只在你的情况下在前台工作,但是,我能做的是给你一个在前台/后台执行的工作示例。
从那里您应该能够找出适合您的解决方案。
同样,请记住,我们不会使用Service
类。
AndroidManifest.xml
<receiver
android:name="com.example.name.Receiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
</intent-filter>
</receiver>
警报.kt
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(context, Receiver::class.java)
// Used for filtering inside Broadcast receiver
intent.action = "MyBroadcastReceiverAction"
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)
// In this particular example we are going to set it to trigger after 30 seconds.
// You can work with time later when you know this works for sure.
val msUntilTriggerHour: Long = 30000
val alarmTimeAtUTC: Long = System.currentTimeMillis() + msUntilTriggerHour
// Depending on the version of Android use different function for setting an
// Alarm.
// setAlarmClock() - used for everything lower than Android M
// setExactAndAllowWhileIdle() - used for everything on Android M and higher
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
alarmManager.setAlarmClock(
AlarmManager.AlarmClockInfo(alarmTimeAtUTC, pendingIntent),
pendingIntent
)
} else {
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
alarmTimeAtUTC,
pendingIntent
)
}
广播接收器.kt
class Receiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// We use this to make sure that we execute code, only when this exact
// Alarm triggered our Broadcast receiver
if (intent?.action == "MyBroadcastReceiverAction") {
Log.d("ALARM", "RECEIVED")
}
}
}
代码在 Kotlin 中,但您在项目中使用它应该没有问题,或者只是重写为 Java。
推荐阅读
- vue.js - 从第三方组件访问组件道具 - vuejs datepicker
- python - Django 错误 __init__() 需要 1 个位置参数,但给出了 2 个
- autosar - AUTOSAR 配置软件(ArcCore Studio):如何解决链接问题
- jquery - Laravel:数据表的 SQLSTATE [23000] 错误
- html - How can I make my container to go down automatically of the page
- javascript - Firestore 批量深拷贝还是浅拷贝?
- c++ - How to remove boilerplate in vector of tuple initialization?
- amazon-web-services - 如何找出我在 AWS 上的实例的 vCPU 和 GB?
- regex - Why `TRegEx.split` returned a empty string as first element?
- database - Unable to execute join with a table and an array on Vertica database