2016-11-19 145 views
-3

我不知道我的代碼中存在什麼問題!當我關閉應用程序時,Android服務不起作用

這是工作,但服務不起作用,當我關閉應用程序!並重新啓動應用程序時重新啓動計數器!任何幫助 我的代碼:

主營:

unit Unit1; 

interface 

uses 
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.IOUtils, 
System.Android.Service, FMX.ScrollBox, FMX.Memo, FMX.Controls.Presentation, 
FMX.StdCtrls 
; 

type 
TForm1 = class(TForm) 
Button1: TButton; 
Memo1: TMemo; 
Button2: TButton; 
procedure Button1Click(Sender: TObject); 
procedure Button2Click(Sender: TObject); 
private 
{ Private declarations } 
FService : TLocalServiceConnection; 
public 
{ Public declarations } 
end; 

var 
    Form1: TForm1; 

implementation 

    {$R *.fmx} 

procedure TForm1.Button1Click(Sender: TObject); 
begin 

FService := TLocalServiceConnection.Create; 

    end; 

procedure TForm1.Button2Click(Sender: TObject); 
begin 
    FService.StartService('AndroidServer'); 


    end; 

    end. 

服務:

unit Unit2; 

    interface 

     uses 
    System.SysUtils, System.Threading, 
    System.Classes, System.IOUtils, 
    System.Android.Service, 
     AndroidApi.JNI.GraphicsContentViewText, 
    Androidapi.JNI.Os, System.Notification; 

    type 

     TAndroidServiceDM = class(TAndroidService) 
     NotificationCenter1: TNotificationCenter; 
     function AndroidServiceStartCommand(const Sender: TObject; 
     const Intent: JIntent; Flags, StartId: Integer): Integer; 
     procedure AndroidServiceCreate(Sender: TObject); 
     Procedure DoNotification; 
      procedure AndroidServiceTaskRemoved(const Sender: TObject; 
       const ARootIntent: JIntent); 
      procedure AndroidServiceDestroy(Sender: TObject); 
     private 
     { Private declarations } 

     public 
     { Public declarations } 

     end; 

     var 
     AndroidServiceDM: TAndroidServiceDM; 

     io:Integer; 
     implementation 

     {%CLASSGROUP 'FMX.Controls.TControl'} 

      {$R *.dfm} 
      uses 
      Androidapi.JNI.App; 

      procedure TAndroidServiceDM.DoNotification; 
      Var 

     MyNotification: TNotification; 



    begin 


    TTask.Run(procedure 
     begin 

    myNotification := NotificationCenter1.CreateNotification; 
     try 
    MyNotification.Name := 'ServiceNotification'; 
    MyNotification.Title := 'Android Service Notification'; 
    MyNotification.AlertBody := io.ToString; 
    MyNotification.FireDate := Now; 
    NotificationCenter1.PresentNotification(MyNotification); 
    finally 
    myNotification.DisposeOf; 
    end; 

    tthread.Synchronize(nil, 
    procedure 
    begin 
    end); 
    end 
    ) ; 

    end; 

    procedure TAndroidServiceDM.AndroidServiceCreate(Sender: TObject); 
     var 
     atask : Itask; 

     begin 
     io:=1; 
     atask := Ttask.create(procedure() 

     begin 

     while true do 
     begin 

     begin 
     sleep(5000); 
     DoNotification; 
     io:=io+1; 
     end; 

     end; 

end); 

atask.Start; 




    end; 

    procedure TAndroidServiceDM.AndroidServiceDestroy(Sender: TObject); 
    begin 

    end; 

    function TAndroidServiceDM.AndroidServiceStartCommand(const Sender:   TObject; 
    const Intent: JIntent; Flags, StartId: Integer): Integer; 
    begin 
    Result := TJService.JavaClass.START_STICKY; 

     end; 



    end. 

它的工作,但是當我關閉應用程序服務無法正常工作! 和計數器重新啓動時再次啓動應用程序! 任何幫助

感謝

+0

有沒有這樣的德爾福版本XE10。德爾福10西雅圖和德爾福10.1德國柏林,都不是XE10。 –

+0

Delphi 10西雅圖和德爾福10.1柏林 –

回答

2

在同一進程中啓動它的應用程序一個當地服務運行。所以當應用程序終止時,服務也是如此。

如果您需要長時間運行的服務超出其宿主應用程序,請在主應用程序中的工作線程中啓動服務,而不是主線程,然後在服務的onStartCommand事件中調用Service.startForeground()方法。

或者,您可能需要將服務標記爲遠程而不是本地清單中的服務,以便服務在其自己的進程中運行。然後,您的主應用程序可以根據需要與服務進行通信,例如使用Intents。

+0

@IsamBuk在互聯網上有很多例子。還有大量關於這個廣泛項目所需的每個特定任務的教程。不要指望我們這裏的任何人爲你寫你的項目。 –

+0

謝謝你能給我例子鏈接嗎?在Delphi 10.1柏林工作 –

+0

startForeground接受'JNotification',但我不知道如何從'TNotification'獲得'JNotification'。 – nurettin

1
  1. 通過AlarmManager

創建服務爲Remote

  • 啓動服務下面是啓動該服務的應用程序的代碼,並立即自行關閉。

    program PlayerPowerMgr; 
    
    uses 
        AndroidApi.JNI.GraphicsContentViewText, 
        Androidapi.Helpers, 
        Androidapi.JNI.App, 
    
        Androidapi.NativeActivity, 
        Androidapi.AppGlue, 
        Androidapi.JNI.Embarcadero, 
        ServiceUnit in '..\PowerMgrService\ServiceUnit.pas' {DM: TAndroidService}; 
    
    {$R *.res} 
    
    procedure StartService; 
    var 
        Intent: JIntent; 
        PendingIntent: JPendingIntent; 
    const 
        ServicePackageName = 'com.embarcadero.services.PlayerPwrMgrServ'; 
        ApplicationPackageName = 'com.embarcadero.PlayerPowerMgr'; 
    begin 
        Intent := TJIntent.Create; 
        Intent.setClassName(
         StringToJString(ApplicationPackageName), 
         StringToJString(ServicePackageName) 
        ); 
        PendingIntent := TJPendingIntent.JavaClass.getService(TAndroidHelper.Context, 1, Intent, 0); 
        TAndroidHelper.AlarmManager.&set(TJAlarmManager.JavaClass.RTC_WAKEUP, 1, PendingIntent); 
    end; 
    
    
    begin 
        StartService; 
        TJFMXNativeActivity.Wrap(PAndroid_app(PANativeActivity(System.DelphiActivity)^.instance)^.activity.clazz).finish; 
    end. 
    

    當然,在項目經理的幫助下,您必須將該服務添加到您的應用程序中。這條線將出現

    ServiceUnit in '..\PowerMgrService\ServiceUnit.pas' {DM: TAndroidService}; 
    

    對於近處的應用程序中使用:

    TJFMXNativeActivity.Wrap(PAndroid_app(PANativeActivity(System.DelphiActivity)^.instance)^.activity.clazz).finish; 
    

    它相當於

    MainActivity.Finish 
    

    ,但應用程序APK的大小,將大約10M少。

    爲了創建這樣的應用

    1)創建應用程序作爲多設備的應用程序/坯料應用

    2)在項目管理器從applicrion除去1單元。

    3)在項目經理選擇應用程序,然後單擊「View Source」,然後將這些代碼

    4)添加您服務應用

  • 相關問題