-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocationService.cs
More file actions
62 lines (58 loc) · 2.16 KB
/
LocationService.cs
File metadata and controls
62 lines (58 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Android.App;
using Android.Content;
using Android.Locations;
using Android.OS;
using Android.Runtime;
using AndroidX.Core.App;
using AndroidX.Lifecycle;
namespace SensorLab {
[Register("world.cryville.sensorlab.LocationService")]
public class LocationService : Service {
public readonly static MutableLiveData LocationData = new MutableLiveData();
const int _notificationId = 1;
static string _notificationTitle;
NotificationManager _ntfMgr;
LocationManager _locMgr;
LocationReceiver _locRecv;
public override void OnCreate() {
base.OnCreate();
_ntfMgr = (NotificationManager)GetSystemService(NotificationService);
_notificationTitle = Resources.GetString(Resource.String.app_name);
_ntfMgr.CreateNotificationChannel(new NotificationChannel("location", Resources.GetString(Resource.String.location), NotificationImportance.Default));
StartForeground(_notificationId, BuildNotification(Resources.GetString(Resource.String.location_fixing)));
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) {
_locMgr = (LocationManager)GetSystemService(LocationService);
_locRecv = new LocationReceiver(OnLocationChanged);
_locMgr.RequestLocationUpdates(
LocationManager.GpsProvider,
new LocationRequest.Builder(1000)
.SetQuality((int)LocationRequestQuality.HighAccuracy)
.Build(),
MainExecutor,
_locRecv
);
return StartCommandResult.Sticky;
}
public override void OnDestroy() {
base.OnDestroy();
_locMgr.RemoveUpdates(_locRecv);
StopForeground(StopForegroundFlags.Remove);
StopSelf();
}
public override IBinder OnBind(Intent intent) => null;
void OnLocationChanged(Location location) {
_ntfMgr.Notify(_notificationId, BuildNotification(string.Format("{0} {1}",
Util.ToDmsString(location.Latitude, "S", "N"),
Util.ToDmsString(location.Longitude, "W", "E")
)));
LocationData.SetValue(location);
}
Notification BuildNotification(string text) => new NotificationCompat.Builder(this, "location")
.SetContentTitle(_notificationTitle)
.SetContentText(text)
.SetOngoing(true)
.SetSmallIcon(Resource.Mipmap.ic_launcher)
.Build();
}
}