Hello,
Welcome to our Microsoft Q&A platform!
You can try to use foreground service to get the location. And add location permission.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
[assembly: Xamarin.Forms.Dependency(typeof(DependentService))]
namespace XamarinA10ForegroundService.Droid
{
[Service]
public class DependentService : Service, IService
{
public void Start()
{
var intent = new Intent(Android.App.Application.Context,
typeof(DependentService));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
Android.App.Application.Context.StartForegroundService(intent);
}
else
{
Android.App.Application.Context.StartService(intent);
}
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
// From shared code or in your PCL
CreateNotificationChannel();
string messageBody = "service starting";
var notification = new Notification.Builder(this, "10111")
.SetContentTitle("Foreground")
.SetContentText(messageBody)
.SetSmallIcon(Resource.Drawable.main)
.SetOngoing(true)
.Build();
StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
MessagingCenter.Subscribe<MainPage, string>(this, "MapIntentReceived", (sender, arg) => {
Toast.MakeText(Android.App.Application.Context, arg, ToastLength.Short).Show();
// await SearchForRooms(arg);
});
Loop();
//do you work
return StartCommandResult.Sticky;
}
async Task<string> Loop()
{
int KGPSTimeout = 5;
Location LowLoc = null;
try
{
//await Geolocation.GetLastKnownLocationAsync();
var GeoRequest = new GeolocationRequest(GeolocationAccuracy.High);
for (int i = 0; i < 5; i++)
{
Location Loc = null;
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(KGPSTimeout));
Loc = await Geolocation.GetLocationAsync(GeoRequest, cts.Token);
if (Loc == null) { continue; }
if (LowLoc == null) { LowLoc = Loc; }
else
{
if (Loc.Accuracy < LowLoc.Accuracy) { LowLoc = Loc; }
if (LowLoc.Accuracy < 10) { break; }
}
Console.WriteLine("================" + Loc.Latitude + "=================");
}
GeoRequest = null;
if (LowLoc != null)
{
var s = await CreateMomCookies(LowLoc);
return s;
}
else { return "NoGPS"; }
}
catch { return "NoGPS"; }
}
async Task<String> CreateMomCookies(Location pos)
{
string acc, alt;
var percentage = Battery.ChargeLevel * 100;
try { acc = pos.Accuracy?.ToString("0"); }
catch { acc = "1000"; }
try { alt = (pos.Altitude * 3.2808399)?.ToString("0"); }
catch { alt = "N/A"; }
var s = pos.Latitude.ToString("0.000000") + "," +
pos.Longitude.ToString("0.000000") + "," +
pos.Timestamp.ToLocalTime().ToString("hh:mm:ss tt") + "," +
acc + "," + alt + "," + percentage.ToString("0") + ",MomData";
await Task.Delay(1);
// Toast.MakeText(Android.App.Application.Context, s, ToastLength.Short).Show();
Console.WriteLine("================"+ s + "=================");
return s;
}
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channelName = Resources.GetString(Resource.String.channel_name);
var channelDescription = GetString(Resource.String.channel_description);
var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)
{
Description = channelDescription
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
}
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.