Hi Everyone,
I'm pretty new to Xamarin Forms Development.
I'm using Background Location Service to track the location from background for every X minutes and Repeating Alarm to send the locations tracks to API. It is working fine in few mobiles and not in few. (Android only)
I tested in two mobile phones which are having Android 7 OS. Working fine in one mobile only.
The one which is not working does not have space.
So, Is there any space concerns for Background location service or Alarm Manger?
Here is my code for Location Track Service
Binding Location
locationRequest = new LocationRequest();
locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
locationRequest.SetInterval(frequency * 60000);
locationRequest.SetFastestInterval(frequency * 60000);
fusedLocationProviderClient = LocationServices.GetFusedLocationProviderClient(this);
fusedLocationProviderClient.RequestLocationUpdates(locationRequest, GetPendingIntent());
private PendingIntent GetPendingIntent()
{
Intent intent = new Intent(this, typeof(MyLocationService));
intent.SetAction(MyLocationService.ACTION_PROCESS_LOCATION);
return PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.UpdateCurrent);
}
Location Broadcast Receiver
[BroadcastReceiver]
public class MyLocationService : BroadcastReceiver
{
public static string ACTION_PROCESS_LOCATION = "LocationTrack.UPDATE_LOCATION";
public static event EventHandler<int> LocationUpdatedEvent;
public override void OnReceive(Context context, Intent intent)
{
if (intent != null)
{
string action = intent.Action;
if (action.Equals(ACTION_PROCESS_LOCATION))
{
LocationResult result = LocationResult.ExtractResult(intent);
if (result != null)
{
var location = result.LastLocation;
var locString = location.Longitude.ToString() + "/" + location.Latitude.ToString();
}
}
}
}
}
Code for Alarm Manager
void StartAlarmService()
{
string MessageText = "Message notification";
var date = DateTime.Now.ToString("MM'-'dd'-'yyyy");
var time = DateTime.Now.ToString("HH:mm");
var dateTime = date + " " + time;
var selectedDateTime = DateTime.ParseExact(dateTime, "MM-dd-yyyy HH:mm", CultureInfo.InvariantCulture);
if (!string.IsNullOrEmpty(MessageText))
{
DependencyService.Get<ILocalNotificationService>().Cancel(0);
DependencyService.Get<ILocalNotificationService>().LocalNotification("Local Notification", MessageText, 0, selectedDateTime);
}
}
public void LocalNotification(string title, string body, int id, DateTime notifyTime)
{
//long repeateDay = 1000 * 60 * 60 * 24;
long repeateForMinute = 300000; // In milliseconds - 15 Min
long totalMilliSeconds = (long)(notifyTime.ToUniversalTime() - _jan1st1970).TotalMilliseconds;
if (totalMilliSeconds < JavaSystem.CurrentTimeMillis())
{
totalMilliSeconds = totalMilliSeconds + repeateForMinute;
}
var intent = CreateIntent(id);
var localNotification = new LocalNotification();
localNotification.Title = title;
localNotification.Body = body;
localNotification.Id = id;
localNotification.NotifyTime = notifyTime;
if (_notificationIconId != 0)
{
localNotification.IconId = _notificationIconId;
}
else
{
localNotification.IconId = Resource.Drawable.abc_btn_check_material;
}
var serializedNotification = SerializeNotification(localNotification);
intent.PutExtra(ScheduledAlarmHandler.LocalNotificationKey, serializedNotification);
Random generator = new Random();
_randomNumber = generator.Next(100000, 999999).ToString("D6");
var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.Immutable);
bool alarmUp = (PendingIntent.GetBroadcast(Application.Context, 0, new Intent(Utilities._packageName), PendingIntentFlags.NoCreate) != null);
if (!alarmUp)
{
var alarmManager = GetAlarmManager();
alarmManager.SetRepeating(AlarmType.RtcWakeup, totalMilliSeconds, repeateForMinute, pendingIntent);
}
}
Can anyone help me what wrong I've done.
Thanks in Advance.