locked
Regular Time Backgroung Process in Xamarin Forms RRS feed

  • Question

  • User89515 posted

    There is anyway to do a cross platform solution for a background activity that runs in a regular time interval? For example to get notifications and things like that from a server...

    I found a topic in StackOverFlow about it: http://stackoverflow.com/questions/28566661/xamarin-forms-background-thread

    but not sure if it works, and Im a little bit lost...

    Someone could help?

    How I could achieve this?

    Monday, June 15, 2015 5:52 PM

All replies

  • User181 posted

    Use the Task Parallel Library.

    Monday, June 15, 2015 7:14 PM
  • User89515 posted

    @adamkemp Thanks for the answer! With TPL can I keep running the process even if the app is minimized? Could you help with any example? Tks

    Monday, June 15, 2015 10:55 PM
  • User181 posted

    First, there's no such thing as "minimized" in mobile applications. The word you're looking for is "background" (as in, you want to run code while your app is in the background).

    In order for your app to continue running when in the background on iOS you need to read this. Note that there are only a few use cases in which your app is allowed to do this.

    Monday, June 15, 2015 11:03 PM
  • User89515 posted

    @adamkemp Tks... Yeah I know that I need background process... however Im looking for any type of cross platform solution... like the one I linked from stackOverflow... Are there such thing like this? If not... if anyone could help with any example for implementing it on each platform with code-specific would help very much!

    Monday, June 15, 2015 11:13 PM
  • User181 posted

    Task Parallel Library is cross platform, but you still have to satisfy the iOS specific requirements to allow your app to keep running. That's an inherently platform-specific thing.

    Monday, June 15, 2015 11:54 PM
  • User181 posted

    Based on the SO post I'm fairly confident that what you're wanting to do isn't allowed for iOS. You can do what's called "background refresh", which gives your application time to occasionally refresh data, but this runs on the OS's schedule. You can't just keep running code continuously because that kills the battery. So again this is just not going to be a cross platform solution.

    Monday, June 15, 2015 11:58 PM
  • User89515 posted

    @adamkemp In fact what I have to do is a process that sends the geo position to a server every hour for example... And get notifications if there are any... If you can, it would be very helpful some sample code on how to manage this type of background process in iOS and Android

    Tuesday, June 16, 2015 3:34 AM
  • User181 posted

    You should really read that link I gave you above. It has a section specifically about location tracking. I don't have sample code. What I have is the documentation, which you should just go read.

    Tuesday, June 16, 2015 3:46 AM
  • User89515 posted

    Thanks @adamkemp I will read and try to implement this in iOS... if anyone else have some sample code, it would help... Specially for android which is my focus right now!

    Thank you very much

    Tuesday, June 16, 2015 4:15 AM
  • User89515 posted

    To solve this on Android Im trying to follow this tutorial and make it work with forms...

    However Im having some problems:

    My LocationServiceClass:

        [Service]public class LocationService : Service, ILocationListener
        {
            IBinder binder;
            public override IBinder OnBind (Intent intent)
            {
                binder = new LocationServiceBinder (this);
                return binder;
            }
    
            public override StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
            {
                return StartCommandResult.Sticky;
            }
    
            protected LocationManager locMgr = Android.App.Application.Context.GetSystemService ("location") as LocationManager;
    
            public void StartLocationUpdates () {       
                var locationCriteria = new Criteria();                    
                locationCriteria.Accuracy = Accuracy.NoRequirement;        
                locationCriteria.PowerRequirement = Power.NoRequirement;                    
                var locationProvider = locMgr.GetBestProvider(locationCriteria, true);
                locMgr.RequestLocationUpdates(locationProvider, 2000, 0, this);
            }
    
            public event EventHandler<LocationChangedEventArgs> LocationChanged = delegate { };
    
            public void OnLocationChanged (Android.Locations.Location location)
            {
                this.LocationChanged (this, new LocationChangedEventArgs (location));
                //Do stuff I need to do every time interval set on RequestLocationUpdates
            }
        }
    

    my LocationServiceBinder:

    public class LocationServiceBinder : Binder
        {
            public LocationService Service
            {
                get { return this.service; }
            } protected LocationService service;
    
            public bool IsBound { get; set; }            
            public LocationServiceBinder (LocationService service) { this.service = service; }
    
    
        }
    

    my LocationServiceConnection

        public class LocationServiceConnection : Java.Lang.Object, IServiceConnection
        {
    
            public LocationServiceConnection (LocationServiceBinder binder)
            {
                if (binder != null) {
                    this.binder = binder;
                }
            }
    
            public void OnServiceConnected (ComponentName name, IBinder service)
            {
                LocationServiceBinder serviceBinder = service as 
                    LocationServiceBinder;
    
                if (serviceBinder != null) {
                    this.binder = serviceBinder;
                    this.binder.IsBound = true;
    
                    // raise the service bound event
                    this.ServiceConnected(this, new ServiceConnectedEventArgs () { Binder = service } );
                    //?
    
                    // begin updating the location in the Service
                    serviceBinder.Service.StartLocationUpdates();
                }
            }
    
            public void OnServiceDisconnected (ComponentName name) { this.binder.IsBound = false; }
        }
    

    my problem begins here! it does not have this.binder and ServiceConnected.... I could declare a binder this way to solve the this.binder:

            public LocationServiceBinder Binder
            {
                get { return this.binder; }
                set { this.binder = value; }
            }
    
            protected LocationServiceBinder binder;
    

    but could not manage to solve the ServiceConnection... Found on gitHub an generic example but didnt have success...

    After it I tried to declare in my shared code App Class this:

            public App ()
            {
                #if __ANDROID__     
    
    
                new System.Threading.Tasks.Task ( () > { 
                    Android.App.Application.Context.StartService (new Intent (Android.App.Application.Context, typeof(LocationService)));
    
                    this.LocationServiceConnection = new LocationServiceConnection (null);
                    this.locationServiceConnection.ServiceConnected += (object sender, ServiceConnectedEventArgs e) => {
                        this.LocationServiceConnected ( this, e );
                    };
    
                    Android.Content.Intent locationServiceIntent = new Android.Content.Intent (Android.App.Application.Context, typeof(LocationService));
                    Android.App.Application.Context.BindService (locationServiceIntent, App.Droid.LocationServiceConnection, Android.Content.Bind.AutoCreate);    
    
                } ).Start ();
    
                #endif
    
    
                // The root page of your application
                MainPage = GetMainPage ();
            }
    

    So I could to the step 10 of the tutorial in my mainActivity on Android project but I had lots of errors...

    Someone could give some advise?

    Tuesday, June 16, 2015 6:55 PM
  • User181 posted

    You'll be better off posting this question in the Android forums.

    Tuesday, June 16, 2015 7:00 PM
  • User89515 posted

    @adamkemp I`m trying to make it work with Forms... this is the reason I posted here

    Tuesday, June 16, 2015 8:01 PM
  • User181 posted

    I know, but your latest post was specifically about an Android implementation. You're going to find a lot more Android expertise in the Android forum.

    Tuesday, June 16, 2015 8:04 PM
  • User89515 posted

    To future searches I opened a topic in Android Forums as suggested by @adamkemp ! Thanks

    Tuesday, June 16, 2015 9:55 PM
  • User89714 posted

    @RaphaelChiorlinRanieri - If you have a subscription to Xamarin University, there are useful courses on there about background processing on both iOS and Android ( but not the poor cousin WinPhone :-( )

    Tuesday, June 16, 2015 10:11 PM
  • User89515 posted

    @JohnHardman Unfortunately I don't have :(


    If I use the approach of stack Overflow would it continue to run even if in background?

        private bool Sync() { //Does stuffs }
    
        private void SynchronizeData()
        {
            // Repeats sync process every 35s
            Device.StartTimer (new TimeSpan (0, 0, 35), Sync);
        }
    
        // Is called pretty much when app starts.
        private void StartSynchronizationThread()
        {
            Task.Factory.StartNew(SynchronizeData, 
                CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
        }
    
    Wednesday, June 17, 2015 5:22 PM
  • User89515 posted

    Hello, If anyone needs in future, just noticed that the tutorial that I refered earlier had a example code with this I was able to solve my problem to android... Later on will try also to iOS tks for the help!

    Saturday, June 20, 2015 4:45 AM