Android Question Service triggers Button unexpectedly

Johan Schoeman

Expert
Licensed User
Longtime User
A little bit lost here:

1. I am working with a PDA3506 (Android handheld PDA)
2. I have the following service called s1:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Service_Create
   
    Log("IN SERVICE_CREATE")

End Sub

Sub Service_Start (StartingIntent As Intent)
   
    If StartingIntent.Action = "com.qs.scancode" Then
        Log("SI = " & StartingIntent.GetExtra("code"))
        CallSubDelayed2("Main", "scanned_value", StartingIntent.GetExtra("code"))
    End If
       
'    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)      
   
End Sub

Sub Service_Destroy

End Sub

3. B4A Manifest looks like this:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:name, "com.qs.wiget.App")

AddApplicationText(<service android:name="com.qs.service.ScanService" >
        </service>)
       
AddReceiverText(s1,
<intent-filter>
    <action android:name="com.qs.scancode" />
</intent-filter>)

4. The PDA has a BIG button named SCAN (laser scanner).
5. When I start the app on the PDA3506 all is working well and when I use the SCAN button I can log the result of the scan in the event of the B4A project's Main activity:
B4X:
Sub scanned_value(barcode As String)
   
    Log("in main and barcode = " & barcode)
   
End Sub

6. The B4A log looks as follows when I use the device's SCAN button to scan 1D/2D codes:
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
IN ACTIVITY_RESUME
** Receiver (s1) OnReceive **
*** Service (s1) Create ***
IN SERVICE_CREATE
** Service (s1) Start **
SI = Hello all of you. Hope you are well?
in main and barcode = Hello all of you. Hope you are well?
** Receiver (s1) OnReceive **
** Service (s1) Start **
SI = Hello all of you. Hope you are well?
in main and barcode = Hello all of you. Hope you are well?
** Receiver (s1) OnReceive **
** Service (s1) Start **
SI = Hello all of you. Hope you are well?
in main and barcode = Hello all of you. Hope you are well?
** Receiver (s1) OnReceive **
** Service (s1) Start **
SI = Hello all of you. Hope you are well?
in main and barcode = Hello all of you. Hope you are well?
** Receiver (s1) OnReceive **
** Service (s1) Start **
SI = Hello all of you. Hope you are well?
in main and barcode = Hello all of you. Hope you are well?
** Receiver (s1) OnReceive **
** Service (s1) Start **
SI = 123456789
in main and barcode = 123456789
** Receiver (s1) OnReceive **
** Service (s1) Start **
SI = 123456789
in main and barcode = 123456789
** Receiver (s1) OnReceive **
** Service (s1) Start **
SI = Hello all of you. Hope you are well?
in main and barcode = Hello all of you. Hope you are well?

7. So, all above is working fine. I also have a B4A button that can create a QR Code or a Code128 barcode. The B4A code:
B4X:
Sub Button3_Click
   
    Log("BUTTON 3 CLICKED")
    Dim bm As Bitmap = prt.create2dBarcode("Hello all of you. Hope you are well?", 300, 300)
    ImageView1.Bitmap = bm
   
End Sub

The method called in the library to create the QR code:
B4X:
    public Bitmap create2dBarcode(String barcodestring, int mWidth, int mHeight) {
        mBitmap = BarcodeCreater.encode2dAsBitmap(barcodestring, mWidth, mHeight, 2);
        return mBitmap;

    }

8. Once I have created the QR code (or the Code128 barcode) and then use the SCAN button of the device, Button3_Click of the B4A project gets triggered/fired. This is what I see in the B4A log:
[CODE
** Receiver (s1) OnReceive **
** Service (s1) Start **
SI = Hello all of you. Hope you are well?
BUTTON 3 CLICKED
in main and barcode = Hello all of you. Hope you are well?
[/CODE]

9. I have renamed Button3 to Button1 but then Button1 gets triggered.

10. I also have buttons 2, 4, and 5 in the B4A project but they never get triggered. Only Button1 or Button3 or whatever else I name the button that creates the QR Code - never the button (in this case Button4) that creates the Code128 barcode.

Why will the button be triggered/fired in the first place when I use the SCAN button of the device? I guess my Service and Manifest might have something to do with this?

Will appreciate some advise on how to solve this.

Thanks

JS
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
My guess is the button is focused and the barcode reader behaves as a keyboard. Set the focus on an EditText and see what happens.
Thanks Erel - that seems to have done the trick.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…