我找到了一个解决方案:
我更新UniDAC组件柏林最新版本(6.3.12)。
TUniConnection和TUniQuery可以很好的与Android Service中的SQLite结合使用。
添加到项目 - >部署主机应用程序我的SQLite DB文件,远程路径设置为“。\ assets \ internal \”。
我希望这段代码对你有用。
procedure TDM.conSQLiteBeforeConnect(Sender: TObject);
begin
{$IF DEFINED(iOS) or DEFINED(ANDROID)}
conSQLite.Database := TPath.Combine(TPath.GetDocumentsPath, 'mybase.sqlite');
{$ENDIF}
end;
procedure TDM.conSQLiteError(Sender: TObject; E: EDAError; var Fail: Boolean);
begin
Log('--- DB error: %s:', [E.Message]);
Fail := False;
end;
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
try
conSQLite.Disconnect;
Log('- DB disconnected', []);
except
on E: Exception do
Log('- can not to disconnect DB', [E.Message]);
end;
Log('... service to be stoped', []);
JavaService.stopSelf;
Result := TJService.JavaClass.START_NOT_STICKY; // don't reload service
end
else
begin
Log('... service started', []);
try
conSQLite.Connect;
Log('+ DB connected', []);
UniQuery.SQL.Text := 'select count(*) as ALLREC from orders';
UniQuery.Open;
if UniQuery.RecordCount > 0 then
begin
UniQuery.First;
Log('... record count: %s', [UniQuery.FieldByName('ALLREC').AsString]);
end;
UniQuery.Close;
except
on E: Exception do
Log('- can not to connect DB: %s', [E.Message]);
end;
Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
end;
end;
如果您希望您的Sqlite位于Android设备上,那么您需要一个在Android上运行的Sqlite版本(如果存在)。 ADO是Windows-only afaik,顺便说一句。另一种方式可能是编写一个Windows托管的REST服务器,并让您的Android应用程序写入该服务器。 – MartynA
那么,如果主应用程序关闭,我可以做些什么,但它的服务正在运行?如果我将消息从服务发送到应用程序(如Intent或其他人),应用程序将运行,而我不需要它。 –