Reputation: 29
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?
Upvotes: 0
Views: 864
Reputation: 597051
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}
Upvotes: 1