In the decument https://developer.android.com/about/versions/14/behavior-changes-14#background-activity-restrictions
you can see that.
For apps targeting Android 14, the system further restricts when apps are allowed to start activities from the background:
When an app sends a PendingIntent using PendingIntent#send() or similar methods, the app must now opt in if it wants to grant its own background activity launch privileges to start the pending intent. To opt in, the app should pass an ActivityOptions bundle with setPendingIntentBackgroundActivityStartMode(MODE_BACKGROUND_ACTIVITY_START_ALLOWED).
So i tried that case with service like this.
class TimeChangeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_TIME_CHANGED ||
intent.action == Intent.ACTION_TIMEZONE_CHANGED ||
intent.action == Intent.ACTION_TIME_TICK ||
intent.action == "android.intent.action.TIME_SET"
) {
val serviceIntent = Intent(context, TimeChangeService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent)
} else {
context.startService(serviceIntent)
}
}
}
}
class TimeChangeService : Service() {
private var timeChangeReceiver: TimeChangeReceiver? = null
val CHANNEL_ID = "ForegroundServiceChannel"
override fun onCreate() {
super.onCreate()
timeChangeReceiver = TimeChangeReceiver()
}
@RequiresApi(34)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val noti = createNotification()
//use notification foreground service
startForeground(1, noti.build())
val intent = Intent(this, MainActivity2::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_MUTABLE, options.toBundle()).
// use send function
pendingIntent.send()
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
timeChangeReceiver?.let {
unregisterReceiver(it)
}
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
private fun createNotification(): NotificationCompat.Builder {
val channelId = "my_channel_id"
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId, "My Channel", NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
val notification = NotificationCompat.Builder(this, channelId)
.setContentTitle("Foreground Service")
.setContentText("Foreground Service is running")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentIntent(pendingIntent)
return notification
}
}
if user change time or date, broadcastReceiver get action and start serivce.
in service,there is start activity with pendingIntent and i think that case is start activity from background but when i change the date, pendingIntent.send() not work no error code either( related with MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
can you explain how can i make sample case properly?