Background Service Location Changed Not Fired Xamarin Android - xamarin

I need to retrieve the real-time location of the device, I had implement ILocationListener in my Service Class as I want this to run even after the app is in the background or closed I have referred - this
and it seems not working for me even if I change my device location the location change event is not fired
My Service Class with Implemented ILocationListener
[Service]
public class LocationService : Service, ILocationListener
{
public event EventHandler<LocationChangedEventArgs> LocationChanged = delegate { };
public event EventHandler<ProviderDisabledEventArgs> ProviderDisabled = delegate { };
public event EventHandler<ProviderEnabledEventArgs> ProviderEnabled = delegate { };
public event EventHandler<StatusChangedEventArgs> StatusChanged = delegate { };
public LocationService()
{
}
// Set our location manager as the system location service
protected LocationManager LocMgr = Android.App.Application.Context.GetSystemService ("location") as LocationManager;
readonly string logTag = "LocationService";
IBinder binder;
public override void OnCreate ()
{
base.OnCreate ();
Log.Debug (logTag, "OnCreate called in the Location Service");
}
// This gets called when StartService is called in our App class
[Obsolete("deprecated in base class")]
public override StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
{
Log.Debug (logTag, "LocationService started");
return StartCommandResult.Sticky;
}
// This gets called once, the first time any client bind to the Service
// and returns an instance of the LocationServiceBinder. All future clients will
// reuse the same instance of the binder
public override IBinder OnBind (Intent intent)
{
Log.Debug (logTag, "Client now bound to service");
binder = new LocationServiceBinder (this);
return binder;
}
// Handle location updates from the location manager
public void StartLocationUpdates ()
{
//we can set different location criteria based on requirements for our app -
//for example, we might want to preserve power, or get extreme accuracy
var locationCriteria = new Criteria();
locationCriteria.Accuracy = Accuracy.NoRequirement;
locationCriteria.PowerRequirement = Power.NoRequirement;
// get provider: GPS, Network, etc.
var locationProvider = LocMgr.GetBestProvider(locationCriteria, true);
Log.Debug (logTag, string.Format ("You are about to get location updates via {0}", locationProvider));
// Get an initial fix on location
LocMgr.RequestLocationUpdates(locationProvider, 2000, 0, this);
Log.Debug (logTag, "Now sending location updates");
}
public override void OnDestroy ()
{
base.OnDestroy ();
Log.Debug (logTag, "Service has been terminated");
// Stop getting updates from the location manager:
LocMgr.RemoveUpdates(this);
}
My Binder Class
public class LocationServiceBinder : Binder
{
public LocationService Service
{
get { return this.service; }
} protected LocationService service;
public bool IsBound { get; set; }
// constructor
public LocationServiceBinder (LocationService service)
{
this.service = service;
}
}
public class LocationServiceConnection : Java.Lang.Object, IServiceConnection
{
public event EventHandler<ServiceConnectedEventArgs> ServiceConnected = delegate {};
public LocationServiceBinder Binder
{
get { return this.binder; }
set { this.binder = value; }
}
protected LocationServiceBinder binder;
public LocationServiceConnection (LocationServiceBinder binder)
{
if (binder != null) {
this.binder = binder;
}
}
// This gets called when a client tries to bind to the Service with an Intent and an
// instance of the ServiceConnection. The system will locate a binder associated with the
// running Service
public void OnServiceConnected (ComponentName name, IBinder service)
{
// cast the binder located by the OS as our local binder subclass
LocationServiceBinder serviceBinder = service as LocationServiceBinder;
if (serviceBinder != null) {
this.binder = serviceBinder;
this.binder.IsBound = true;
Log.Debug ( "ServiceConnection", "OnServiceConnected Called" );
// raise the service connected event
this.ServiceConnected(this, new ServiceConnectedEventArgs () { Binder = service } );
// now that the Service is bound, we can start gathering some location data
serviceBinder.Service.StartLocationUpdates();
}
}
// This will be called when the Service unbinds, or when the app crashes
public void OnServiceDisconnected (ComponentName name)
{
this.binder.IsBound = false;
Log.Debug ( "ServiceConnection", "Service unbound" );
}
}
public class ServiceConnectedEventArgs : EventArgs
{
public IBinder Binder { get; set; }
}
My MainActivity
public class MainActivity : Activity
{
readonly string logTag = "MainActivity";
// make our labels
TextView latText;
TextView longText;
TextView altText;
TextView speedText;
TextView bearText;
TextView accText;
#region Lifecycle
//Lifecycle stages
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
Log.Debug (logTag, "OnCreate: Location app is becoming active");
SetContentView (Resource.Layout.Main);
// This event fires when the ServiceConnection lets the client (our App class) know that
// the Service is connected. We use this event to start updating the UI with location
// updates from the Service
App.Current.LocationServiceConnected += (object sender, ServiceConnectedEventArgs e) => {
Log.Debug (logTag, "ServiceConnected Event Raised");
// notifies us of location changes from the system
App.Current.LocationService.LocationChanged += HandleLocationChanged;
//notifies us of user changes to the location provider (ie the user disables or enables GPS)
App.Current.LocationService.ProviderDisabled += HandleProviderDisabled;
App.Current.LocationService.ProviderEnabled += HandleProviderEnabled;
// notifies us of the changing status of a provider (ie GPS no longer available)
App.Current.LocationService.StatusChanged += HandleStatusChanged;
};
latText = FindViewById<TextView> (Resource.Id.lat);
longText = FindViewById<TextView> (Resource.Id.longx);
altText = FindViewById<TextView> (Resource.Id.alt);
speedText = FindViewById<TextView> (Resource.Id.speed);
bearText = FindViewById<TextView> (Resource.Id.bear);
accText = FindViewById<TextView> (Resource.Id.acc);
altText.Text = "altitude";
speedText.Text = "speed";
bearText.Text = "bearing";
accText.Text = "accuracy";
// Start the location service:
App.StartLocationService();
}
protected override void OnPause()
{
Log.Debug (logTag, "OnPause: Location app is moving to background");
base.OnPause();
}
protected override void OnResume()
{
Log.Debug (logTag, "OnResume: Location app is moving into foreground");
base.OnResume();
}
protected override void OnDestroy ()
{
Log.Debug (logTag, "OnDestroy: Location app is becoming inactive");
base.OnDestroy ();
// Stop the location service:
App.StopLocationService();
}
#endregion
#region Android Location Service methods
///<summary>
/// Updates UI with location data
/// </summary>
public void HandleLocationChanged(object sender, LocationChangedEventArgs e)
{
Android.Locations.Location location = e.Location;
Log.Debug (logTag, "Foreground updating");
// these events are on a background thread, need to update on the UI thread
RunOnUiThread (() => {
latText.Text = String.Format ("Latitude: {0}", location.Latitude);
longText.Text = String.Format ("Longitude: {0}", location.Longitude);
altText.Text = String.Format ("Altitude: {0}", location.Altitude);
speedText.Text = String.Format ("Speed: {0}", location.Speed);
accText.Text = String.Format ("Accuracy: {0}", location.Accuracy);
bearText.Text = String.Format ("Bearing: {0}", location.Bearing);
});
}
public void HandleProviderDisabled(object sender, ProviderDisabledEventArgs e)
{
Log.Debug (logTag, "Location provider disabled event raised");
}
public void HandleProviderEnabled(object sender, ProviderEnabledEventArgs e)
{
Log.Debug (logTag, "Location provider enabled event raised");
}
public void HandleStatusChanged(object sender, StatusChangedEventArgs e)
{
Log.Debug (logTag, "Location status changed, event raised");
}
#endregion
}
the above public void HandleLocationChanged(object sender, LocationChangedEventArgs e) {}
never get fired on my device location change

Related

Is it possible to Fetch user location from a Worker class?

I have to schedule a work to fetch user current location and update to server in a given interval (Even the app is not running).
I am trying to WorkManagerAPI to implement the functionality.
Is it possible to fetch the current location of the user from the doWork() method ?
locationManager.requestLocationUpdates(
provider, timeInterval, travelDistance, locationListener
);
When I request Location updates from the doWork() it throws below error.
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
As per my understanding, when implementing LocationManager.requestLocationUpdates() on a Worker thread, the call is being made on a non-UI, background thread created by WorkManager. LocationManager.requestLocationUpdates() is an asynchronous call possibly on another background thread. To handle the callbacks defined by the LocationListener, the calling thread must stay alive. Thats why the exception says,
Can't create handler inside thread that has not called Looper.prepare()
Check the code snippet below. Please consider this as pseudocode, I haven't tested this piece of code.
public class LocationWorker extends Worker {
String LOG_TAG = "LocationWorker";
private Context mContext;
private MyHandlerThread mHandlerThread;
public LocationWorker(#NonNull Context context, #NonNull WorkerParameters workerParams) {
super(context, workerParams);
mContext = context;
}
#NonNull
#Override
public Result doWork() {
Log.d(LOG_TAG, "doWork");
mHandlerThread = new MyHandlerThread("MY_THREAD");
mHandlerThread.start();
Runnable runnable = new Runnable() {
#Override
public void run() {
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
String bestProvider = locationManager.getBestProvider(new Criteria(), true);
boolean permission = false;
if (PermissionChecker.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
PermissionChecker.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.e(LOG_TAG, "This app requires ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions.");
permission = true;
}
Log.d(LOG_TAG, "permission: "+permission);
Log.d(LOG_TAG, "bestProvider: "+bestProvider);
if (permission && bestProvider != null) {
MyLocationListener locListener = new MyLocationListener();
locationManager.requestLocationUpdates(bestProvider, 500, 1, locListener, mHandlerThread.getLooper());
}
}
};
mHandlerThread.post(runnable);
return Result.success();
}
class MyHandlerThread extends HandlerThread {
Handler mHandler;
MyHandlerThread(String name) {
super(name);
}
#Override
protected void onLooperPrepared() {
Looper looper = getLooper();
if (looper != null)
mHandler = new Handler(looper);
}
void post(Runnable runnable) {
if (mHandler != null)
mHandler.post(runnable);
}
}
class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(final Location loc)
{
Log.d(LOG_TAG, "Location changed: " + loc.getLatitude() +","+ loc.getLongitude());
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d(LOG_TAG, "onStatusChanged");
}
#Override
public void onProviderDisabled(String provider)
{
Log.d(LOG_TAG, "onProviderDisabled");
}
#Override
public void onProviderEnabled(String provider)
{
Log.d(LOG_TAG, "onProviderEnabled");
}
}
}

Android Service binding with MvvmCross

I am developing xamarin.Android app in MvvmCross. I want to call a service even when the App is backgrounded and a user is logged in. The problem is, I want to call this service within every say 2 hours whether the app is in foreground or background, just the user of the App needs to be logged in.
Intent loggedintent = new Intent(this,typeof(DeviceLoginHelper));
loggedintent.PutExtra("LoggedIn", true);
StartService(loggedintent);
I have written an android service:
[Service]
public class DeviceLoginHelper : IntentService
{
protected override void OnHandleIntent(Intent intent)
{
try
{
if(intent.HasExtra("LoggedIn"))
{
}
}
catch(Exception ex) { }
}
}
But how can I implement a timer? Where do I initialise and handle event to the timer. And when timer is elapsed when should I call ?
public override void OnDestroy()
{
try
{
base.OnDestroy();
}
catch(Exception ex){}
}
and when a user loges out i want to stop this service. Where do I put the call StopService() in MvvmCross
I would not use a Timer. Instead you should configure the AlarmManager.
[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{
private static AlarmManager alarmMgr;
private static PendingIntent alarmIntent;
public const int NOTIFICATION_ID = 1;
public const int IDLE_TIME_MS = 30 * 1000; // 30-seconds (update here)
private NotificationManager mNotificationManager;
Notification.Builder builder;
public override void OnReceive(Context context, Intent intent)
{
// Do something when alarm triggers (here I'm building notification)
BuildNotification(context);
// reschedule alarm
ScheduleAlarm(IDLE_TIME_MS);
}
public static Context ApplicationContext { get; set; }
public static void ScheduleAlarm(int milliseconds)
{
if (milliseconds == 0) return;
alarmMgr = (AlarmManager)ApplicationContext.GetSystemService(Context.AlarmService);
var intent = new Intent(ApplicationContext, typeof(AlarmReceiver));
alarmIntent = PendingIntent.GetBroadcast(ApplicationContext, 0, intent, 0);
alarmMgr.Set(AlarmType.ElapsedRealtimeWakeup,
SystemClock.ElapsedRealtime() + milliseconds, alarmIntent);
}
private void BuildNotification(Context context)
{
mNotificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
var contentIntent = PendingIntent.GetActivity(context, 0, new Intent(context, typeof(MainView)), 0);
var message = $"Time is up";
var mBuilder = new Notification.Builder(context)
.SetAutoCancel(true)
.SetPriority(NotificationCompat.PriorityMax)
.SetDefaults(NotificationDefaults.All)
.SetContentTitle("Time is up")
.SetStyle(new Notification.BigTextStyle()
.BigText(message))
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.ic_launcher);
mBuilder.SetContentIntent(contentIntent);
mNotificationManager.Notify(NOTIFICATION_ID, mBuilder.Build());
}
}
In your startup code, simply call:
AlarmReceiver.ApplicationContext = context;
AlarmReceiver.ScheduleAlarm(timeInMs);

How can we make app in kiosk mode using xamarin?

I'm creating new app using xamarin. I have already completed some part using some sample codes. I'm able to disable back buttons, volume buttons and power button.
But when trying to disable home button I'm getting error on debugging.
I'm following this link,Kiosk mode in Andriod.
But when trying to disable home button I'm getting error on debugging.
Since you didn't post your code and your error message, we don't know what happened, I just tried to create such a sample followed the blog your posted and it works fine by my side.
Here is the service:
namespace KioskModeAndroid
{
[Service]
[IntentFilter(new[] { "KioskModeAndroid.KioskService" })]
public class KioskService : Service
{
private static long INTERVAL = Java.Util.Concurrent.TimeUnit.Seconds.ToMillis(2);
private static string TAG = typeof(KioskService).Name;
private static string PREF_KIOSK_MODE = "pref_kiosk_mode";
private Thread t = null;
private Context ctx = null;
private bool running = false;
public override void OnDestroy()
{
Log.Info(TAG, "Stopping service 'KioskService'");
running = false;
base.OnDestroy();
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
Log.Info(TAG, "Starting service 'KioskService'");
running = true;
ctx = this;
t = new Thread(() =>
{
while (running)
{
handleKioskMode();
Thread.Sleep(INTERVAL);
}
StopSelf();
});
t.Start();
return StartCommandResult.NotSticky;
}
private void handleKioskMode()
{
if (isKioskModeActive(ctx))
{
}
if (isInBackground())
{
restoreApp();
}
}
private bool isKioskModeActive(Context context)
{
var sp = PreferenceManager.GetDefaultSharedPreferences(context);
return sp.GetBoolean(PREF_KIOSK_MODE, false);
}
private bool isInBackground()
{
var am = ctx.GetSystemService(Context.ActivityService) as ActivityManager;
var processes = am.RunningAppProcesses;
foreach (var process in processes)
{
if (process.Importance == ActivityManager.RunningAppProcessInfo.ImportanceForeground)
{
foreach (var activeprocess in process.PkgList)
{
if (activeprocess == ctx.PackageName)
return false;
}
}
}
return true;
}
private void restoreApp()
{
Intent i = new Intent(ctx, typeof(MainActivity));
i.AddFlags(ActivityFlags.NewTask);
ctx.StartActivity(i);
}
public override IBinder OnBind(Intent intent)
{
return null;
}
}
}
I started this service in the OnCreate of MainActivity:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
StartService(new Intent(this, typeof(KioskService)));
}

DisplayAlert With changing Text xamarin forms

I have a requirement where i have to show the status of the download on a DisplayAlert. But with changing text on it asynchronously.
How to achieve this?
DisplayAlert("Download Info", "Downloading.....", "Ok");
I want to show status like...
Connected to server
Downloading
Download Complete
Here is a simple "Dynamic Alert" for Forms and iOS using UIAlertController and Android using a DialogFragment and a Xamarin.Forms dependency service:
Dependency Interface:
public interface IDynamicAlert
{
void Show(string title, string message);
void Update(string message);
void Dismiss();
}
iOS IDynamicAlert Dependency Implementation:
public class DynamicAlert : IDynamicAlert
{
UIAlertController alert;
public void Show(string title, string message)
{
if (alert != null) throw new Exception("DynamicAlert already showing");
alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
var rootVC = UIApplication.SharedApplication.Windows[0].RootViewController;
rootVC.PresentViewController(alert, true, () =>
{
});
}
public void Update(string message)
{
if (alert == null) throw new Exception("DynamicAlert is not showing, call Show first");
alert.Message = message;
}
public void Dismiss()
{
if (alert == null) throw new Exception("DynamicAlert is not showing, call Show first");
alert.DismissViewController(true, () =>
{
alert.Dispose();
alert = null;
});
}
}
Example Usage:
var alert = DependencyService.Get<IDynamicAlert>();
if (alert != null)
{
alert.Show("StackOverflow", "Starting your request...");
await Task.Delay(2000); // Do some work...
alert.Update("Your request is processing...");
await Task.Delay(2000); // Do some work...
alert.Update("Your request is complete...");
await Task.Delay(750);
alert.Dismiss();
}
else
{
throw new Exception("IDynamicAlert Dependency not found");
}
Output:
Android Version:
The android version consists of a couple of parts, a DialogFragment subclass and the IDynamicAlert implementation that uses the custom DialogFragment.
Android DialogFragment Subclass:
public class DynamicAlertDialogFragment : DialogFragment
{
AlertDialog alertDialog;
readonly Context context;
public static DynamicAlertDialogFragment Instance(Context context, string title, string message)
{
var fragment = new DynamicAlertDialogFragment(context);
Bundle bundle = new Bundle();
bundle.PutString("title", title);
bundle.PutString("message", message);
fragment.Arguments = bundle;
return fragment;
}
public DynamicAlertDialogFragment(Context context)
{
this.context = context;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
var title = Arguments.GetString("title");
var message = Arguments.GetString("message");
alertDialog = new AlertDialog.Builder(context)
.SetIcon(Android.Resource.Drawable.IcDialogInfo)
.SetTitle(title)
.SetMessage(message)
.Create();
return alertDialog;
}
public void SetMessage(string message)
{
(context as Activity).RunOnUiThread(() => { alertDialog.SetMessage(message);});
}
}
Android IDynamicAlert Dependency Implementation:
public class DynamicAlert : IDynamicAlert
{
const string FRAGMENT_TAG = "DynamicAlert_Fragment";
DynamicAlertDialogFragment fragment;
static FormsAppCompatActivity currentActivity;
public static FormsAppCompatActivity CurrentActivity { set { currentActivity = value; } }
public void Show(string title, string message)
{
if (currentActivity == null) throw new Exception("DynamicAlert.CurrentActivity needs assigned");
var fragMgr = currentActivity.FragmentManager;
var fragTransaction = fragMgr.BeginTransaction();
var previous = fragMgr.FindFragmentByTag(FRAGMENT_TAG);
if (previous != null)
{
fragTransaction.Remove(previous);
}
fragTransaction.DisallowAddToBackStack();
fragment = DynamicAlertDialogFragment.Instance(currentActivity, title, message);
fragment.Show(fragMgr, FRAGMENT_TAG);
}
public void Update(string message)
{
if (fragment == null) throw new Exception("DynamicAlert is not showing, call Show first");
fragment.SetMessage(message);
}
public void Dismiss()
{
if (fragment == null) throw new Exception("DynamicAlert is not showing, call Show first");
fragment.Dismiss();
fragment.Dispose();
fragment = null;
}
}
Android Init / Usage:
When creating the AlertDialog in the DialogFragment we need access to the current Activity and when using Xamarin.Forms, that is normally the MainActivity that is a FormsAppCompatActivity subclass. Thus you will need to initialize the DynamicAlert.CurrentActivity static property with this Activity in your MainActivity.OnCreate subclass:
Example:
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
////////////
DynamicAlert.CurrentActivity = this;
////////////
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
Android Output:

Query Firebase Value OnChange

I am trying to register a Xamarin app to listen for value changes on a particular value change. I can access the value, but for some reason when I listen to the value change, it will fire once but never again until I reboot.
I am using Xamarin Studio, and FireSharp libraries. This is the API code in the C# library portion of the app. The reason I have removed the delegate was to check if it wasn't the delegate being cleaned up after the first call or something.
public class ValueAPI
{
private IFirebaseClient _client;
private ITemperatureListener _listener;
private EventStreamResponse _response;
public ValueAPI()
{
IFirebaseConfig config = new FirebaseConfig
{
AuthSecret = "...",
BasePath = "https://[value-api].firebaseio.com/"
};
_client = new FirebaseClient(config);
}
public async Task<string> getValue()
{
FirebaseResponse response = await _client.GetAsync("VALUE");
return response.Body;
}
public async Task<string> registerForUpdates(IValueListener listener)
{
_listener = listener;
_response = await _client.OnAsync("VALUE", null, this.OnValueChange, null, null);
return _response.ToString();
}
private void OnValueChange(object sender, ValueChangedEventArgs args, object context)
{
if (_listener != null)
{
_listener.OnValueUpdated(args.Data);
}
}
}
ANDROID CODE
ValueAPI api = new ValueAPI();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
getValue();
getValueUpdates();
}
private async void getValue()
{
Task<string> task = api.getValue();
string result = await task;
TextView label = (TextView)FindViewById(Resource.Id.label_value);
label.Text = result;
}
private async void getValueUpdates()
{
Task<string> task = api.registerForUpdates(this);
await task;
}
public void OnValueUpdated(string value)
{
TextView label = (TextView)FindViewById(Resource.Id.label_value_updated);
label.Text = value;
}

Resources