Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch an activity from TileService for Android 14 is not allowed

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?

like image 323
Style-7 Avatar asked Oct 25 '25 05:10

Style-7


2 Answers

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.

like image 65
zomega Avatar answered Oct 26 '25 20:10

zomega


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)
}
like image 38
Sylvester Yao Avatar answered Oct 26 '25 20:10

Sylvester Yao