Home How to parse data from intent in Delphi
Reply: 1

How to parse data from intent in Delphi

MonZon
1#
MonZon Published in 2017-09-13 10:59:23Z

I'm trying to get data from an Intent. It is passed as an array of Float containing one element. I tried this code, but it crashes:

procedure TMainScreen.BroadcastReceiver2Receive(Context: JContext; Intent: JIntent);
{$IFDEF ANDROID}
var
  Temp: String;
  Hr1: single;
  Bundle :Jbundle;
begin
  button2.Text:= 'triggered';
  if Intent.hasExtra(stringtojstring('DATA')) = true then
  begin
    bundle := intent.getExtras();
    button1.Text:= 'got data';
    hr1:= bundle.getFloatArray(stringtojstring('DATA'))[0];
    button2.Text:= floattostr(hr1);
  end;
{$ELSE}
begin
{$ENDIF}
end;

I'm sure I'm not dealing with the array correctly. How do I do it?

Remy Lebeau
2#
Remy Lebeau Reply to 2017-09-13 16:44:43Z

You should be using JIntent.getFloatArrayExtra() instead of JIntent.getExtras().

But, either way, before accessing the array element, you need to check if the returned array is nil or not. If it is not, you should also check that the array length is actually > 0.

Try something more like this:

{$IFDEF ANDROID}
procedure TMainScreen.BroadcastReceiver2Receive(Context: JContext; Intent: JIntent);
var
  DataStr: JString;
  Arr: TJavaArray<Single>;
  Hr1: single;
begin
  Button2.Text := 'triggered';
  DataStr := StringToJString('DATA');
  if Intent.hasExtra(DataStr) then
  begin
    Button1.Text := 'got data';
    Arr := Intent.getFloatArrayExtra(DataStr);
    if (Arr <> nil) and (Arr.Length > 0) then
    begin
      Hr1 := Arr[0];
      Button2.Text := FloatToStr(Hr1);
    end else
      Button2.Text := 'no value';
  end;
end;
{$ENDIF}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.341984 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO