Home Delphi 10.2.2 Tokyo AndroidService CreateNotification Segment Fault 11 Java_com_embarcadero_rtl_ProxyService_onStartCommandNative?
Reply: 0

Delphi 10.2.2 Tokyo AndroidService CreateNotification Segment Fault 11 Java_com_embarcadero_rtl_ProxyService_onStartCommandNative?

user10582
1#
user10582 Published in July 21, 2018, 6:11 am

Errors out ":9EBD03FA Java_com_embarcadero_rtl_ProxyService_onStartCommandNative" My issue is that I can't seem to get my service to send a notification back to my main thread.

Once I can get this working, I'll publish this on github. This is an opensource project along with using Bass Library.

Debugging using - Moto G 4 Play - Android 6.0.1.

When I press the Button1Click to "test" it, I get a segment fault 11. It highlights this code in System.Notification:

function TCustomNotificationCenter.Supported: Boolean;
begin
  Result := FPlatformNotificationCenter <> nil;  <<<---- error here
end;

Here is my onStartCommandNative code from part of my Link to Services.Radio.Service.pas:

function TDM.AndroidServiceStartCommand(const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
var
  MyNotification: TNotification;
begin
  // recieve commands
  if Assigned(Intent) then
  begin
    if Intent.getAction.equalsIgnoreCase(StringToJString('StopIntent')) = true then
    begin
      JavaService.stopSelf;
      Result := TJService.JavaClass.START_NOT_STICKY; // don't reload service
    end
    else if Intent.getAction.equalsIgnoreCase(StringToJString('StartIntent')) = true then
    begin
      FRadyo.SetStreamURL(JStringToString(Intent.getData.toString));
      Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
    end
    else if Intent.getAction.equalsIgnoreCase(StringToJString('GetMetaData')) = true then
    begin
      LaunchNotification;  // <<<--------- issue?
      Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
    end
    else if Intent.getAction.equalsIgnoreCase(StringToJString('GetInfoData')) = true then
    begin
      // notification InfoData
      if InfoData <> '' then
      begin
        MyNotification := NotificationCenter1.CreateNotification;
        try
          MyNotification.Name := 'InfoData';
          MyNotification.Title := 'Broadcast Information';
          MyNotification.AlertBody := InfoData;
          NotificationCenter1.PresentNotification(MyNotification);
        finally
          MyNotification.DisposeOf;
        end;
      end;
      Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
    end
    else if Intent.getAction.equalsIgnoreCase(StringToJString('PlayRadio')) = true then
    begin
      if Assigned(FRadyo)
        then begin
                FRadyo.Play;
        end;
      Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
    end
    else if Intent.getAction.equalsIgnoreCase(StringToJString('PauseRadio')) = true then
    begin
      if Assigned(FRadyo)
        then begin
                FRadyo.Pause;
        end;
      Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
    end
    else Result := TJService.JavaClass.START_STICKY;
  end
  else Result := TJService.JavaClass.START_STICKY;
end;

Here is the LaunchNotification code:

procedure TDM.LaunchNotification;
var
  MyNotification: TNotification;
begin
   MyNotification := NotificationCenter1.CreateNotification;
   try
     MyNotification.Name := 'ServiceNotification';
     MyNotification.Title := 'Android Service Notification';
     MyNotification.AlertBody := 'My Notification works using GetMetaData LOL!';
     MyNotification.FireDate := Now;
     NotificationCenter1.PresentNotification(MyNotification);
   finally
     MyNotification.Free;
   end;
end;

Not all of its listed, but if you follow the above link then you can see all the sourcecode for this project except for my main project using this code.

Here is main project unit1:

unit Unit1;

interface

uses
  System.StartupCopy, System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.Gestures,
  FMX.TabControl, FMX.ScrollBox, FMX.Memo, System.Math.Vectors, FMX.Ani,
  FMX.Controls3D, FMX.Objects3D, FMX.MaterialSources, FMX.Viewport3D,
  System.Notification, System.Android.Service; //, FMX.Types3D;

type
  TForm1 = class(TForm)
    Viewport3D2: TViewport3D;
    Viewport3D1: TViewport3D;
    Memo1: TMemo;
    //TextureMaterialSource1: TTextureMaterialSource;     // PLA logo
    Plane2: TPlane;
    FloatAnimation1: TFloatAnimation;
    Text3D1: TText3D;
    //TextureMaterialSource2: TTextureMaterialSource;     // the square color icocn with lighting bolt
    //TextureMusicPlayIcon: TTextureMaterialSource;       // music play icon
    //TextureMaterialMusicPause: TTextureMaterialSource;  // music pause icon
    PlaneMusicPlayPause: TPlane;
    FloatAnimation2: TFloatAnimation;
    TextureMusicPlayIcon: TTextureMaterialSource;
    TextureMaterialMusicPause: TTextureMaterialSource;
    TextureMaterialSource1: TTextureMaterialSource;
    TextureMaterialSource2: TTextureMaterialSource;
    NotificationCenter1: TNotificationCenter;
    GestureManager1: TGestureManager;
    GridPanelLayout1: TGridPanelLayout;
    ToolBar1: TToolBar;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure PlaneMusicPlayPauseClick(Sender: TObject);
    procedure FloatAnimation2Process(Sender: TObject);
    //procedure GestureDone(Sender: TObject; const EventInfo: TGestureEventInfo;
      //var Handled: Boolean);
    procedure NotificationCenter1ReceiveLocalNotification(Sender: TObject;
      ANotification: TNotification);
    procedure FloatAnimation2Finish(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    //FService: TLocalServiceConnection;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  PlayMusic : Boolean;
  PlayMusicButtonAnimationLock : Boolean;

implementation

{$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID}
{$R *.LgXhdpiPh.fmx ANDROID}
{$R *.LgXhdpiTb.fmx ANDROID}

uses
//  System.Android.Service;
  Androidapi.Helpers,
  Androidapi.JNI.App,
  Androidapi.JNI.Net,  //required by TJnet_Uri
  Androidapi.JNI.GraphicsContentViewText, // for JIntent
  Androidapi.JNI.JavaTypes;  // for JIntent

procedure TForm1.FloatAnimation2Process(Sender: TObject);
var
  LIntent: JIntent;
begin

  if (PlayMusicButtonAnimationLock = false) and (PlaneMusicPlayPause.RotationAngle.Y >= 180) then
  begin
    if PlaneMusicPlayPause.MaterialSource = TextureMusicPlayIcon then
      begin
        PlaneMusicPlayPause.MaterialSource := TextureMaterialMusicPause;
        PlayMusicButtonAnimationLock := true;
        PlayMusic := true;
        FloatAnimation1.Enabled := true;
        LIntent := TJIntent.Create;
        LIntent.setClassName(TAndroidHelper.Context.getPackageName(),
          TAndroidHelper.StringToJString('com.embarcadero.services.AndroidRadioService'));
        LIntent.setAction(StringToJString('PlayRadio'));
        TAndroidHelper.Activity.startService(LIntent);
      end
    else
      begin
        PlaneMusicPlayPause.MaterialSource := TextureMusicPlayIcon;
        PlayMusicButtonAnimationLock := true;
        PlayMusic := false;
        FloatAnimation1.Enabled := false;
        LIntent := TJIntent.Create;
        LIntent.setClassName(TAndroidHelper.Context.getPackageName(),
          TAndroidHelper.StringToJString('com.embarcadero.services.AndroidRadioService'));
        LIntent.setAction(StringToJString('PauseRadio'));
        TAndroidHelper.Activity.startService(LIntent);
      end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  LIntent: JIntent;
begin
        LIntent := TJIntent.Create;
        LIntent.setClassName(TAndroidHelper.Context.getPackageName(),
          TAndroidHelper.StringToJString('com.embarcadero.services.AndroidRadioService'));
        LIntent.setAction(StringToJString('GetMetaData'));
        TAndroidHelper.Activity.startService(LIntent);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  LIntent: JIntent;
begin
        LIntent := TJIntent.Create;
        LIntent.setClassName(TAndroidHelper.Context.getPackageName(),
          TAndroidHelper.StringToJString('com.embarcadero.services.AndroidRadioService'));
        LIntent.setAction(StringToJString('GetInfoData'));
        TAndroidHelper.Activity.startService(LIntent);
end;

procedure TForm1.FloatAnimation2Finish(Sender: TObject);
begin
    FloatAnimation2.Enabled := false;
    PlayMusicButtonAnimationLock := false;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  LIntent: JIntent;
  Data: Jnet_Uri;
begin
  //FService:= TLocalServiceConnection.Create;
  //TLocalServiceConnection.StartService('AndroidRadioService');
  LIntent := TJIntent.Create;
  LIntent.setClassName(TAndroidHelper.Activity.getBaseContext,
    TAndroidHelper.StringToJString('com.embarcadero.services.AndroidRadioService'));
  Data := TJnet_Uri.JavaClass.parse(StringToJString('http://s6.nexuscast.com/tunein/cactiradio.pls'));
  LIntent.setData(Data);
  LIntent.setAction(StringToJString('StartIntent'));
  TAndroidHelper.Activity.startService(LIntent);
  //TabControl1.ActiveTab := TabItem2;
  PlayMusic := false;
  PlayMusicButtonAnimationLock := false;
end;

{
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
  if Key = vkHardwareBack then
  begin
    if (TabControl1.ActiveTab = TabItem1) and (TabControl1.ActiveTab = TabItem2) then
    begin
      TabControl2.Previous;
      Key := 0;
    end;
  end;
end;


procedure TForm1.GestureDone(Sender: TObject; const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
  case EventInfo.GestureID of
    sgiLeft:
      begin
        if TabControl1.ActiveTab <> TabControl1.Tabs[TabControl1.TabCount - 1] then
          TabControl1.ActiveTab := TabControl1.Tabs[TabControl1.TabIndex + 1];
        Handled := True;
      end;

    sgiRight:
      begin
        if TabControl1.ActiveTab <> TabControl1.Tabs[0] then
          TabControl1.ActiveTab := TabControl1.Tabs[TabControl1.TabIndex - 1];
        Handled := True;
      end;
  end;
end; }

procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject;
  ANotification: TNotification);
begin
  Memo1.Lines.Add('Notification received:');
  Memo1.Lines.Add(ANotification.AlertBody);
end;

procedure TForm1.PlaneMusicPlayPauseClick(Sender: TObject);
begin
  FloatAnimation2.Enabled := true;
end;

end.
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO