我想用Delphi做一个Android服务来获取背景位置坐标(Android 8.0 Oreo),但是主机应用程序总是崩溃了,请有人帮我(我疯狂测试很多解决方案,但我找不到) 这是我的代码:
FUNCTION TMyServiceDM.AndroidServiceStartCommand(CONST Sender: TObject;
CONST Intent: JIntent; Flags, StartId: Integer): Integer;
BEGIN
Contador := 0;
LocationSensor1.Active := True;
MStartId := StartId;
MostrarNotificacion;
Result := TJService.JavaClass.START_STICKY;
END;
PROCEDURE TMyServiceDM. MostrarNotificacion;;
VAR
LBuilder: JNotificationCompat_Builder;
Ntf: JNotification;
NtfManager: JNotificationManager;
Intent: JIntent;
PendingIntent: JPendingIntent;
BEGIN
Intent := TJIntent.Create;
Intent.SetClass(TAndroidHelper.Context, TAndroidHelper.Context.GetClass);
Intent.SetFlags(TJIntent.JavaClass.FLAG_ACTIVITY_SINGLE_TOP OR
TJIntent.JavaClass.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent := TJPendingIntent.JavaClass.GetActivity(TAndroidHelper.Context,
0, Intent, TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
LBuilder := TJNotificationCompat_Builder.JavaClass.Init
(TAndroidHelper.Context);
LBuilder.SetDefaults(TJNotification.JavaClass.DEFAULT_LIGHTS);
LBuilder.SetAutoCancel(True);
LBuilder.SetContentTitle(StrToJCharSequence('hola'));
LBuilder.SetContentText(StrToJCharSequence(Contador.Tostring+' '+gLatitude.tostring+' '+gLongitude.tostring);
LBuilder.SetSmallIcon(TAndroidHelper.Context.GetApplicationInfo.Icon);
LBuilder.SetTicker(StrToJCharSequence('123'));
LBuilder.SetWhen(TJDate.Create.GetTime);
Lbuilder.SetContentIntent(PendingIntent);
Ntf := LBuilder.Build;
Ntf.SetLatestEventInfo(TAndroidHelper.Context, StrToJCharSequence('Title'),
StrToJCharSequence('Text'), TJPendingIntent.JavaClass.GetActivity
(TAndroidHelper.Context, 0, Intent, 0));
Ntfmanager := TJNotificationManager.Wrap
((TAndroidHelper.Context.GetSystemService
(TJContext.JavaClass.NOTIFICATION_SERVICE) AS ILocalObject).GetObjectID);
NtfManager.Notify(1, Ntf);
TAndroidHelper.Activity.StartService(Intent);
JavaService.StartForeground(MStartID, Ntf);
END;
PROCEDURE TMyServiceDM.LocationSensor1LocationChanged(Sender: TObject;
CONST OldLocation, NewLocation: TLocationCoord2D);
BEGIN
Inc(Contador);
gLatitude := Newlocation.Latitude;
gLongitude := NewLocation.Longitude;
MostrarNotificacion;
END;
在Android 8.0中奥利奥GPS更新在应用程序进入后台模式时停止,根据页面“developers.android.com”是必要的,发送了一个Android服务,发送带有位置坐标或其他文本的通知并使用“javaservice.startforeground” “