I have a simple TileService and try to launch an activity by click on the tile. It is works on Android 13 and low but in Android 14 I get an exception:
startActivityAndCollapse: Starting activity from TileService using an Intent is not allowed.
How to fix it?
Here is the source code of TileService.java. And here is the documentation of startActivityAndCollapse(Intent intent).
Both say you have to use startActivityAndCollapse(PendingIntent) because startActivityAndCollapse(Intent intent) is deprecated.
You must be using startActivityAndCollapse(Intent intent) at the moment because you are getting this error. Use the other one instead.
We need to do some compatibility processing. The correct and available code is as follows:
val intent = Intent().apply {
component = ComponentName(
"your_pkg_name",
"your_class_name"
)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startActivityAndCollapse(
PendingIntent.getActivity(
appContext,
0,
intent,
PendingIntent.FLAG_IMMUTABLE
)
)
} else {
startActivityAndCollapse(intent)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With