Please help me create a sample about background task in xamarin.forms?

NTL_DevNTL_Dev USMember ✭✭✭

Hi everybody.
In my app need a task auto run every 15min. I have successed with UWP background task.
But I have visit Xamarin Document of X.iOS and X.Android. I can not completed understand it.
Please make a simple sample about background task run every 15min in xamarin.forms
THANK YOU VERY MUCH.

Tagged:

Answers

  • robbitrobbit CNMember ✭✭

    Hi ,@NguyenLiem
    In Android you can use the AlarmManager( under Android 4.4) or JobSchedulerService ( above Android 5.0) or Timer (java Api). Maybe the FCM will help you.
    If there is an task auto run every 15 min , the app should be alive all the time . In android it can't be achieved , because it will consumes electricity very fast . Google doesn't advocate doing that.

  • NTL_DevNTL_Dev USMember ✭✭✭
    edited November 10

    @robbit said:
    Hi ,@NguyenLiem
    In Android you can use the AlarmManager( under Android 4.4) or JobSchedulerService ( above Android 5.0) or Timer (java Api). Maybe the FCM will help you.
    If there is an task auto run every 15 min , the app should be alive all the time . In android it can't be achieved , because it will consumes electricity very fast . Google doesn't advocate doing that.

    I'm creating app for my company, In my app need 2 feature:
    1. Send location to server every 15min (Or all time if can)
    2. Send notification when server have new records of data.
    Please suggest me the best way to do this!
    Thank you!

  • CharwakaCharwaka INMember ✭✭✭

    Hi @NguyenLiem

    Please use following code

    Device.StartTimer(TimeSpan.FromMinutes(15),() => {
    
                    Task.Run(async () =>
                    {
    
    
                 //Write the method you want to run Here
    
                    });
                                  return true; //use this to run continuously 
                                  return false; //to stop running continuously 
    
                });
    
    Make sure you are properly handling **UI Thread**
    
  • NTL_DevNTL_Dev USMember ✭✭✭

    @Charwaka said:
    Hi @NguyenLiem

    Please use following code

    Device.StartTimer(TimeSpan.FromMinutes(15),() => {
    
                    Task.Run(async () =>
                    {
    
                         
                 //Write the method you want to run Here
                            
                    });
                                  return true; //use this to run continuously 
                                  return false; //to stop running continuously 
    
                });
    
    Make sure you are properly handling **UI Thread**
    

    Will it run without open app?

  • CharwakaCharwaka INMember ✭✭✭

    Hi > @NMackay said:

    Use this code to run without even app open

  • NTL_DevNTL_Dev USMember ✭✭✭

    @Charwaka said:
    Hi > @NMackay said:

    Use this code to run without even app open

    I have seen this sample.
    But I'm sorry, my english is terrible, I can not complete understand it.
    Can you help explain in every row of code, thank you!

Sign In or Register to comment.