0

リモート バックグラウンド Android サービスにデータを送信する方法を知っています。

.
.
FServiceConnection: TRemoteServiceConnection;
.
.
procedure TForm1.Button1Click(Sender: TObject);
const
  GET_STRING = 1234;
var
  LMessage: JMessage;
begin
  FServiceConnection := TRemoteServiceConnection.Create;
  FServiceConnection.BindService('Name of the APK containing service', 'Service name');

  LMessage := TJMessage.JavaClass.obtain(nil, GET_STRING);
  LMessage.replyTo := FServiceConnection.LocalMessenger;
  FServiceConnection.ServiceMessenger.send(LMessage);
end;

しかし、ローカルの Android サービスで同じことを行うにはどうすればよいでしょうか。

.
.
FServiceConnection: TLocalServiceConnection;
.
.
procedure TForm1.Button1Click(Sender: TObject);
const
  GET_STRING = 1234;
var
  LMessage: JMessage;
begin
  FServiceConnection := TLocalServiceConnection.Create;
  FServiceConnection.BindService('Service name');

  LMessage := TJMessage.JavaClass.obtain(nil, GET_STRING);
  ????
  ????
end;

または、アプリケーションとバックグラウンド Android ローカル サービスの間でメッセージを送信する別の方法はありますか?

この問題について誰かにアドバイスしますか?

4

3 に答える 3

1

Android Local Service の ServiceStartCommand イベントで作成します。

HOST アプリケーション アクティビティのボタンを押して、JIntent にラップされた「開始コマンド」をサービスに送信します。

procedure TForm3.Button1Click(Sender: TObject);
var
  LIntent: JIntent;
begin
  Log.D('Try to start');
  LIntent := TJIntent.Create;
  LIntent.setClassName(TAndroidHelper.Context.getPackageName(),
    TAndroidHelper.StringToJString('com.embarcadero.services.SvcTimer'));
  LIntent.setAction(StringToJString('StartIntent'));
  TAndroidHelper.Activity.startService(LIntent);
end;

サービスはコマンドを取得し、私の JIntent をテストします。

function TDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent;
  Flags, StartId: Integer): Integer;
begin
  Log('+ START with Intent: ' + JStringToString(Intent.getAction.toString), []);
  if Intent.getAction.equalsIgnoreCase(StringToJString('StopIntent')) then
  begin
    Log('... service to be stoped', []);
...
    JavaService.stopSelf;
    Result := TJService.JavaClass.START_NOT_STICKY; // don't reload service
    Log('- service stoped', []);
  end
  else if Intent.getAction.equalsIgnoreCase(StringToJString('StartIntent')) then
  begin
...
    LocationSensor.Active := True;
    Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
    Log('+ Service started', []);
  end;
end;

その上で、ホスト アプリケーション コマンドを実行します。

于 2016-06-05T16:34:54.630 に答える