Recent

Author Topic: (SOLVED) LAMW - intercept stand by cellphone  (Read 2398 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
(SOLVED) LAMW - intercept stand by cellphone
« on: September 24, 2021, 09:14:53 am »
Hi, using the jZBarcodeScannerView component I noticed that when the phone goes into standby while jZBarcodeScannerView is scanning, the application crashes. How can I solve the problem? Thanks
« Last Edit: September 26, 2021, 11:12:50 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: LAMW - intercept stand by cellphone
« Reply #1 on: September 24, 2021, 06:43:53 pm »
Hi, since I don't know your device I guess the problem and solution.
Not closing the barcode scanner in the stop method of the activity triggered without entering the standby mode, and then not restarting the barcode scanner in the resume method that works when returning to the activity!

Regarding the subject, I made a few minor changes in the AppZBarcodeScannerViewDemo1 sample and made it work smoothly for me.
Tried and Runs in Standby Mode without Problem.

Code: Pascal  [Select][+][-]
  1. {Hint: save all files to location: C:\android\workspace\AppZBarcodeScannerViewDemo1\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, zbarcodescannerview;
  10.  
  11. type
  12.  
  13.   { TAndroidModule1 }
  14.  
  15.   TAndroidModule1 = class(jForm)
  16.     jBitmap1: jBitmap;
  17.     jButton1: jButton;
  18.     jButton2: jButton;
  19.     jTextView1: jTextView;
  20.     jZBarcodeScannerView1: jZBarcodeScannerView;
  21.     procedure AndroidModule1ActivityPause(Sender: TObject);
  22.     procedure AndroidModule1ActivityResume(Sender: TObject);
  23.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  24.     procedure AndroidModule1RequestPermissionResult(Sender: TObject;
  25.       requestCode: integer; manifestPermission: string;
  26.       grantResult: TManifestPermissionResult);
  27.     procedure jButton1Click(Sender: TObject);
  28.     procedure jButton2Click(Sender: TObject);
  29.     procedure jZBarcodeScannerView1Click(Sender: TObject);
  30.     procedure jZBarcodeScannerView1ScannerResult(Sender: TObject;
  31.       codedata: string; codeformat: TBarcodeFormat);
  32.   private
  33.     {private declarations}
  34.   public
  35.     {public declarations}
  36.   end;
  37.  
  38. var
  39.   AndroidModule1: TAndroidModule1;
  40.   Jzbsc:Boolean=false;
  41. implementation
  42.  
  43. {$R *.lfm}
  44.  
  45.  
  46. { TAndroidModule1 }
  47.  
  48. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  49. begin
  50.   jBitmap1.LoadFromAssets('codebar1.png');  //or qrcode1.png
  51.   jZBarcodeScannerView1.Scan(jBitmap1.GetImage());
  52. end;
  53.  
  54. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  55. begin
  56.    if IsRuntimePermissionNeed() then   // that is, if target API >= 23
  57.    begin
  58.      ShowMessage('Requesting Runtime Permission....');
  59.      Self.RequestRuntimePermission('android.permission.CAMERA', 1113);   //handled by OnRequestPermissionResult
  60.    end
  61. end;
  62.  
  63. procedure TAndroidModule1.AndroidModule1ActivityPause(Sender: TObject);
  64. begin
  65.   if Jzbsc then jZBarcodeScannerView1.StopScan();
  66. end;
  67.  
  68. procedure TAndroidModule1.AndroidModule1ActivityResume(Sender: TObject);
  69. begin
  70.   if Jzbsc then jZBarcodeScannerView1.Scan();
  71. end;
  72.  
  73. procedure TAndroidModule1.AndroidModule1RequestPermissionResult(
  74.   Sender: TObject; requestCode: integer; manifestPermission: string;
  75.   grantResult: TManifestPermissionResult);
  76. begin
  77.   case requestCode of
  78.     1113:begin
  79.             if grantResult = PERMISSION_GRANTED  then
  80.                 ShowMessage('Success! "'+ manifestPermission + '" granted!')
  81.             else//PERMISSION_DENIED
  82.                 ShowMessage('Sorry... "android.permission.CAMERA" permission not granted... ' );
  83.          end;
  84.   end;
  85. end;
  86.  
  87. procedure TAndroidModule1.jButton2Click(Sender: TObject);
  88. begin
  89.    if IsRuntimePermissionGranted('android.permission.CAMERA') then
  90.   begin
  91.      jZBarcodeScannerView1.Scan();
  92.      Jzbsc:=true;
  93.   end;
  94. end;
  95.  
  96. procedure TAndroidModule1.jZBarcodeScannerView1Click(Sender: TObject);
  97. begin
  98.   if IsRuntimePermissionGranted('android.permission.CAMERA') then
  99.   begin
  100.      jZBarcodeScannerView1.Scan();
  101.   end;
  102. end;
  103.  
  104. procedure TAndroidModule1.jZBarcodeScannerView1ScannerResult(Sender: TObject;
  105.   codedata: string; codeformat: TBarcodeFormat);
  106. begin
  107.   ShowMessage('codedata = '+ codedata + '    ::    codeformat = ' + IntToStr(Ord(codeformat)));
  108. end;
  109.  
  110. end.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: LAMW - intercept stand by cellphone
« Reply #2 on: September 24, 2021, 07:33:34 pm »
Quote
I made a few minor changes in the AppZBarcodeScannerViewDemo1 ...

Updated in  LAMW github!

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: LAMW - intercept stand by cellphone
« Reply #3 on: September 26, 2021, 11:12:37 pm »
Perfect. Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018