Android Question [ SOLVED] Target SDK 31 widget crash

Cebuvi

Active Member
Licensed User
Longtime User
Hello,

I am upgrading a widget-enabled app to targetSDK 31.
When I install the widget, it does not load and I get an error.

B4X:
java.lang.RuntimeException: Unable to start receiver com.tempusumbra.solarwidget.solarw4x2$solarw4x2_BR: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.tempusumbra.solarwidget/.solarw4x2

Can anyone help me with a solution?

Thanks
 

Cebuvi

Active Member
Licensed User
Longtime User
As I still can't find a solution, I have tried to start with something simple and from the beginning. With the example of Erel's HomeWidgets.
If I set targetSDK=31, I get this error.

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Receiver (widgetservice) OnReceive **
*** Service (widgetservice) Create ***
widgetservice_service_create (java line: 180)
java.lang.IllegalArgumentException: anywheresoftware.b4a.samples.homewidgets: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
    at android.app.PendingIntent.checkFlags(PendingIntent.java:382)
    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:673)
    at android.app.PendingIntent.getBroadcast(PendingIntent.java:660)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:84)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:77)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:77)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.createRemoteViews(RemoteViewsWrapper.java:58)
    at anywheresoftware.b4a.samples.homewidgets.widgetservice._service_create(widgetservice.java:180)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.samples.homewidgets.widgetservice.onCreate(widgetservice.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:4940)
    at android.app.ActivityThread.access$1800(ActivityThread.java:310)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2300)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
java.lang.RuntimeException: Unable to create service anywheresoftware.b4a.samples.homewidgets.widgetservice: java.lang.RuntimeException: java.lang.IllegalArgumentException: anywheresoftware.b4a.samples.homewidgets: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:4953)
    at android.app.ActivityThread.access$1800(ActivityThread.java:310)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2300)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)

If I keep the line Service.StopAutomaticForeground, the same error occurs as I indicated in the previous post.

Has anyone else tried the widgets in targetSDK=31 and is having problems?
 
Last edited:
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
I have received an email from Google with information about new app requirements and among them is a reference to API versions. And as you can see in the attached image, to update an app from November you have to have targetSDK=31 and I have started testing.
2022-08-05_121711.jpg
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
With SDK 30 everything works correctly. I simply changed the manifest to TargetSDK=31 and the errors appeared.

As the app uses StartServiceAtExact, I first added AddPermission(android.permission.SCHEDULE_EXACT_ALARM), because otherwise I got an error telling me that I had to add the instruction.

Thanks
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
As I indicated above it's a breaking change when targeting SDK 31 that needs Erel's attention
Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
ConfigureHomeWidgets isn't an actual method in core.jar, it looks like a placeholder for the compiler to replace with a call to RemoteViewsWrapper.createRemoteViews.

I think loadLayoutHelper in RemoteViewsWrapper needs modifying to at least set a required flag in its PendingIntent when targeting SDK31 and maybe other changes too.

There is no way to hack this without Erel's involvement.
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
Thank you very much for the reply and the information. It is not an issue that is in a hurry but Erel will be able to solve it.
 
Upvote 0

ilpesce

Member
Licensed User
Longtime User
Greetings unfortunately the problem has become very urgent ... it is no longer possible to publish sdk30 apps I can't publish an app that contains widgets.
 
Upvote 0

ilpesce

Member
Licensed User
Longtime User
Unfortunately the sdk31 + widget problems are not over

java.lang.RuntimeException: Unable to start receiver sagenut.widget.example.widget_4x1$widget_4x1_BR: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service sagenut.widget.example/.widget_4x1
at android.app.ActivityThread.handleReceiver(ActivityThread.java:4321)
at android.app.ActivityThread.access$1600(ActivityThread.java:247)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2068)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service sagenut.widget.example/.widget_4x1
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelable(Parcel.java:3333)
at android.os.Parcel.createExceptionOrNull(Parcel.java:2420)
at android.os.Parcel.createException(Parcel.java:2409)
at android.os.Parcel.readException(Parcel.java:2392)
at android.os.Parcel.readException(Parcel.java:2334)
at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:5971)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1847)
at android.app.ContextImpl.startForegroundService(ContextImpl.java:1823)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:779)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:779)
at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.startServiceFromReceiver(ServiceHelper.java:141)
at sagenut.widget.example.widget_4x1$widget_4x1_BR.onReceive(widget_4x1.java:17)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:4312)
... 9 more
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
I have tried updating my app and now everything seems to work perfectly. The app doesn't stop, it allows me to add the widgets and they work as with SDK 30.

I think that's solved. Thank you very much Erel for your great work.
 
Upvote 0

ilpesce

Member
Licensed User
Longtime User
thanks, but it doesn't improve
it seems that the widget can no longer digest
Service.StopAutomaticForeground ecc. it is necessary to delete them all and leave the management completely to the operating system and suffer the relative notification that seems to no longer go away...(maybe?‍♂️)
 
Upvote 0
Top