Android Question [RESOLVED] SDK 34, Broadcast Receiver, Unable to start.

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

I will try to be very specific in relation to this issue. I have a service, svc_br_receiver, and in that service I use broadcast receiver lib. The lib has a method called registerReceiver. Under SDK 34 it fails with the following:
B4X:
svc_br_receiver_service_create (java line: 300)
java.lang.SecurityException: com.islesystems.atlasbtn: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
    at android.os.Parcel.createExceptionOrNull(Parcel.java:3087)
    at android.os.Parcel.createException(Parcel.java:3071)
    at android.os.Parcel.readException(Parcel.java:3054)
    at android.os.Parcel.readException(Parcel.java:2996)
    at android.app.IActivityManager$Stub$Proxy.registerReceiverWithFeature(IActivityManager.java:5684)
    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1868)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1804)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1792)
    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:765)
    at com.rootsoft.broadcastreceiver.BroadCastReceiver.registerReceiver(BroadCastReceiver.java:52)
    at com.islesystems.atlasbtn.svc_br_receiver._service_create(svc_br_receiver.java:300)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at com.islesystems.atlasbtn.svc_br_receiver.onCreate(svc_br_receiver.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:4915)
    at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2447)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:257)
    at android.os.Looper.loop(Looper.java:368)
    at android.app.ActivityThread.main(ActivityThread.java:8826)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:14900)
    at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2570)
    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3005)
    at android.os.Binder.execTransactInternal(Binder.java:1387)
    at android.os.Binder.execTransact(Binder.java:1299)

I have looked this up on the web and found the following; https://stackoverflow.com/questions...r-not-exported-should-be-specified-when-a-rec

I do not know if the lib is supported anymore https://www.b4x.com/android/help/broadcastreceiver.html

The intents I need to receive dont work when they are in the manifest (static) only in a service(dynamic). There are no background services, notifications etc, just intent receiving.

Suggestions ?

Regards

John.
 
Solution
You are asking about this library: https://www.b4x.com/android/forum/threads/broadcastreceiver.12493/#content

Use this sub to register the receiver instead of calling br.RegisterReceiver:
B4X:
Private Sub RegisterReceiver(br As BroadCastReceiver, Exported As Boolean)
    Dim p As Phone
    If p.SdkVersion < 33 Then
        br.registerReceiver("")
    Else
        Dim r As Reflector
        r.Target = br
        Dim filter As Object = r.GetField("filter")
        Dim receiver As Object = r.GetField("receiver")
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        ctxt.RunMethod("registerReceiver", Array(receiver, filter, IIf(Exported, 2, 4)))
        r.SetField("isRegistered", "true", "java.lang.boolean")
    End If...

Jmu5667

Well-Known Member
Licensed User
Longtime User
Manifest file.

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="34"/>
                 <supports-screens     android:largeScreens="true" 
                                    android:normalScreens="true" 
                                     android:smallScreens="true" 
                                    android:anyDensity="true"/>
                )

SetApplicationAttribute(android:theme, @android:style/Theme.Holo.Light)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

AddApplicationText(<provider
                          android:name="android.support.v4.content.FileProvider"
                          android:authorities="$PACKAGE$.provider"
                          android:exported="false"
                          android:grantUriPermissions="true">
                      <meta-data
                          android:name="android.support.FILE_PROVIDER_PATHS"
                          android:resource="@xml/provider_paths"/>
                      </provider>
                )
                
CreateResource(xml, provider_paths,
                    <paths>
                           <external-files-path name="name" path="" />
                           <files-path name="name" path="" />
                           <files-path name="name" path="shared" />
                    </paths>
                )

' // samsung - https://docs.samsungknox.com/dev/knox-sdk/hardware-key-remapping-isv.htm#mag_0
AddApplicationText(<meta-data
                      android:name="com.samsung.android.knox.intent.action.HARD_KEY_PRESS"
                      android:value="true" />
                    <receiver android:name=".br_receiver$br_receiver_BR" android:exported="true">
                          <intent-filter>
                            <action android:name="com.samsung.android.knox.intent.action.HARD_KEY_REPORT" />
                          </intent-filter>
                    </receiver>
                    )

AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission(android.permission.READ_PHONE_STATE)

' // 
' // PTT Buttons
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.CHANNELDOWN.down"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.CHANNELUP.down"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.PTT.down"/></intent-filter>)            
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.PTT.up"/></intent-filter>)    
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.ptt.down"/></intent-filter>)    
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.ptt.up"/></intent-filter>)    
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.chivin.action.MEDIA_PTT_DOWN"/></intent-filter>)    
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.chivin.action.MEDIA_PTT_UP"/></intent-filter>)    



' // 2020.05.04 - M31 Android 10
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.intent.ptt.down"/></intent-filter>)            
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.intent.ptt.up"/></intent-filter>)    
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.intent.SOS.down"/></intent-filter>)            
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.intent.SOS.up"/></intent-filter>)    

    
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.dfl.a9.camdown"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.dfl.a9.camup"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.dfl.ptt.longpress"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.hebs.action.ACTION_START_PTT"/></intent-filter>)            
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.hebs.action.ACTION_STOP_PTT"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.zello.ptt.down"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.zello.ptt.up"/></intent-filter>)

AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.KEYEVENT_DOWN_131"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.KEYEVENT_DOWN_132"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.KEYEVENT_DOWN_133"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.KEYEVENT_DOWN_134"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.KEYEVENT_UP_131"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.KEYEVENT_UP_132"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.KEYEVENT_UP_133"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.KEYEVENT_UP_134"/></intent-filter>)    
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.SCANER_KEYEVENT_DOWN"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.SCANER_KEYEVENT_UP"/></intent-filter>)        
AddReceiverText(br_receiver, <intent-filter> <action android:name="shmaker.android.intent.action.SCANER_KEYEVENT_LONG"/></intent-filter>)    


' // SOS Buttons
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.CALL_FOR_HELP"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.ENTER_ONEKEYSOS"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.EXIT_ONEKEYSOS"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.SOS.down"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.SOS.longpress"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.SOS.now"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.SOS.up+"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.SOS_BUTTON"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.sos"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.sos.down"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.sos.up"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="android.intent.action.sos_ticket"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.BrighterWirelessMain.Main.SOSDown"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.BrighterWirelessMain.Main.SOSUp"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.Toughshield.Main.SOSDown"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.Toughshield.Main.SOSUp"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.action.F3_SOS"/></intent-filter>)    
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.doro.partner.action.ASSISTANCE_BUTTON_ACTIVATED"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.tdc.callforhelp.CallForHelpService"/></intent-filter>)

' // 2023.08.21
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.sos.down"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.sos.up"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.scan.down"/></intent-filter>)
AddReceiverText(br_receiver, <intent-filter> <action android:name="com.android.scan.up"/></intent-filter>)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are asking about this library: https://www.b4x.com/android/forum/threads/broadcastreceiver.12493/#content

Use this sub to register the receiver instead of calling br.RegisterReceiver:
B4X:
Private Sub RegisterReceiver(br As BroadCastReceiver, Exported As Boolean)
    Dim p As Phone
    If p.SdkVersion < 33 Then
        br.registerReceiver("")
    Else
        Dim r As Reflector
        r.Target = br
        Dim filter As Object = r.GetField("filter")
        Dim receiver As Object = r.GetField("receiver")
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        ctxt.RunMethod("registerReceiver", Array(receiver, filter, IIf(Exported, 2, 4)))
        r.SetField("isRegistered", "true", "java.lang.boolean")
    End If
End Sub

Depends on Phone, Reflection, JavaObject libraries.
Note that I haven't fully tested it.

Explanation about EXPORTED:
 
Upvote 1
Solution

Jmu5667

Well-Known Member
Licensed User
Longtime User
To unregister the receiver with SDK34

B4X:
Sub unRegisterReceiver(pBR As BroadCastReceiver)

    Dim p As Phone
    If p.SdkVersion < 33 Then
        pBR.unregisterReceiver
    Else
        Dim r As Reflector
        r.Target = pBR
        Dim receiver As Object = r.GetField("receiver")
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        ctxt.RunMethod("unregisterReceiver", Array(receiver))
        r.SetField("isRegistered", "false", "java.lang.boolean")
    End If
End Sub
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
I think that the regular BR.unregisterReceiver will also work. No need for special code.
I had that in code already and it failed with receiver not register, so I used unRegisterReceiver without any issues. Thanks for your help on this one.
 
Upvote 0
Top