I’m trying to create a standalone alarm app for my watch (Pixel Watch 2). It was going smooth until I needed to show the screen (Activity
) when the alarm goes off to show a snooze button. I tried many different things like:
- Passing
PendingIntent
withActivity
to theAlarmManager
- Passing
PendingIntent
withBroadcastReceiver
- Which tries to start an
Activity
(no crash here, but also noActivity
shown) - Which tries to show a notification with
setFullScreenIntent
(notification shows,Activity
does not) - Which starts a Foreground Service which shows a notification with
setFullScreenIntent
(notification shows,Activity
does not)
- Which tries to start an
- Passing
PendingIntent
withService
(crashes because I can’t use a Foreground Service if passed like this)
So I’m not sure what to try anymore. I’ve tried all variations with different Intent Flags and nothing happens. Only thing that does work is if I use a PendingIntent
with Activity
and keep the app open, the Activity
will launch (same happens when starting from the BroadcastReceiver
.
I’m not sure what kind of code helps, so I’ll show passing the PendingIntent
with Activity
:
<!-- Part of AndroidManifest -->
<activity
android:name=".activities.AlarmActivity"
android:exported="false"
android:showWhenLocked="true"
android:taskAffinity=""
android:noHistory="true"
android:excludeFromRecents="true"
android:launchMode="singleTop"
android:turnScreenOn="true" />
val Context.alarmManager: AlarmManager
get() = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
fun setAlarm(context: Context, millis: Long) {
val alarmManager = context.alarmManager
if(!alarmManager.canScheduleExactAlarms()) {
return
}
val alarmActivity = Intent(context, AlarmActivity::class.java).also {
it.flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
context.alarmManager.setAlarmClock(
AlarmManager.AlarmClockInfo(
millis,
PendingIntent.getActivity(context, 1, Intent(context, MainActivity::class.java), PendingIntent.FLAG_IMMUTABLE)
),
PendingIntent.getActivity(context, 2, alarmActivity, PendingIntent.FLAG_IMMUTABLE)
)
}
class AlarmActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
logd("on create alarm activity")
setTheme(android.R.style.Theme_DeviceDefault)
setContent {
MyTheme {
Box(modifier = Modifier.fillMaxSize()) {
Text("Alarm!!")
}
}
}
setTurnScreenOn(true)
}
}
I’m out of ideas, so if someone has an idea to point me in the right direction, that would be much appreciated.
You need to sign in to view this answers
Leave feedback about this