Receive intent from another app in delphi android

  

I’m trying to get the data from Sleep as Android app (actually, Mi Band Tools is sending them). It uses com.urbandroid.sleep.watch.HR_DATA_UPDATE intent. I can easily see this intent being sent every minute (heart rate defined period) with Intent Listener app. But how can I grab the data from it myself?

My try on this is solely based on Embarcadero Receive Intent example. But it closes (or crashes) on receiving intent. Which is a good thing – it does detect it at least =)

And the second part – I’ll need to grab this data somehow. I know that intent sends this as array of float.

Any help really appreciated. Thanks!

Here is my manifest file

<?xml version=”1.0″ encoding=”utf-8″?>
<!– BEGIN_INCLUDE(manifest) –>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”%package%”
android:versionCode=”%versionCode%”
android:versionName=”%versionName%”
android:installLocation=”%installLocation%”>

<!– This is the platform API where NativeActivity was introduced. –>
<uses-sdk android:minSdkVersion=”%minSdkVersion%” android:targetSdkVersion=”%targetSdkVersion%” />
<%uses-permission%>
<uses-feature android:glEsVersion=”0x00020000″ android:required=”True”/>
<application android:persistent=”%persistent%”
android:restoreAnyVersion=”%restoreAnyVersion%”
android:label=”%label%”
android:debuggable=”%debuggable%”
android:largeHeap=”%largeHeap%”
android:icon=”%icon%”
android:theme=”%theme%”
android:hardwareAccelerated=”%hardwareAccelerated%”>

<%application-meta-data%>
<!– Our activity is a subclass of the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. –>

<%activity%>
<receiver android:name=”com.embarcadero.firemonkey.notifications.FMXNotificationAlarm”>
<intent-filter>
<action android:name=”com.urbandroid.sleep.watch.HR_DATA_UPDATE” /> <!–Constant Value for the ACTION_VIEW–>
<data android:mimeType=”data” /> <!–Sets the MIME type of your intent.–>
</intent-filter>
</receiver>

<%receivers%>
</application>
</manifest>
<!– END_INCLUDE(manifest) –>

My unit.pas:

//—————————————————————————
// Copyright (c) 2016 Embarcadero Technologies, Inc. All rights reserved.
//
// This software is the copyrighted property of Embarcadero Technologies, Inc.
// (“Embarcadero”) and its licensors. You may only use this software if you
// are an authorized licensee of Delphi, C++Builder or RAD Studio
// (the “Embarcadero Products”). This software is subject to Embarcadero’s
// standard software license and support agreement that accompanied your
// purchase of the Embarcadero Products and is considered a Redistributable,
// as such term is defined thereunder. Your use of this software constitutes
// your acknowledgement of your agreement to the foregoing software license
// and support agreement.
//—————————————————————————
unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Messaging, FMX.Platform,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, Androidapi.JNI.GraphicsContentViewText,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.WebBrowser, FMX.Objects, FMX.ScrollBox, FMX.Memo;

type
TForm1 = class(TForm)
ToolBar1: TToolBar;
Layout1: TLayout;
Memo1: TMemo;
Label1: TLabel;
CornerButton1: TCornerButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
function HandleIntentAction(const Data: JIntent): Boolean;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.fmx}

uses FMX.Platform.Android, Androidapi.JNI.JavaTypes, Androidapi.JNI.Net, Androidapi.JNI.Os, Androidapi.Helpers;

procedure TForm1.FormCreate(Sender: TObject);
var
AppEventService: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, AppEventService) then
AppEventService.SetApplicationEventHandler(HandleAppEvent);

// Register the type of intent action that we want to be able to receive.
// Note: A corresponding <action> tag must also exist in the <intent-filter> section of AndroidManifest.template.xml.
MainActivity.registerIntentAction(StringtoJstring(‘com.urbandroid.sleep.watch.HR_DATA_UPDATE’));
TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, HandleActivityMessage);
end;

procedure TForm1.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
if M is TMessageReceivedNotification then
HandleIntentAction(TMessageReceivedNotification(M).Value);
// Memo1.Lines.Add(‘Gotcha!’);
end;

function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
var
StartupIntent: JIntent;
begin
Result := False;
if AAppEvent = TApplicationEvent.BecameActive then
begin
StartupIntent := MainActivity.getIntent;
if StartupIntent <> nil then
HandleIntentAction(StartupIntent);
end;
end;

function TForm1.HandleIntentAction(const Data: JIntent): Boolean;
var
Extras: JBundle;
hr_data: array[0..1] of real;
begin
Result := False;

// Memo1.Lines.Add(‘Gotcha2!’);
if Data <> nil then
begin
// Memo1.ClearContent;
Extras := Data.getExtras;
if Extras <> nil then showmessage(‘Gotcha!’);
// hr_data[0] := (Extras.getFloatArray(StringtoJstring(‘DATA’)));
// Invalidate;
end;
end;

end.

Comments are closed.