locked
How can I run a certain job in background even though after I close my app? RRS feed

  • Question

  • User388416 posted

    I am creating a mobile app for employee to check in. After check in, I want their location can be keep tracked all the time like 30 minutes interval. In the other words, every 30 minutes, the location of the mobile phone will be sent to the server through calling a WebService. It is impossible for me to ask them open the mobile app and screen all day long right? So I want to have a background process that will run inside the mobile phone? How can I achieve that? The concept is just like the alarm in the mobile phone, how it knows when to ring everyday even though the user does not open the alarm app? I have tried alarmManager but unfortunately nothing works. Thanks in advance.

    Wednesday, January 8, 2020 8:37 AM

All replies

  • User379860 posted

    In the android side. If you want to access the server every 30 minutes, you can use the Job scheduler to achieve that. https://docs.microsoft.com/en-us/xamarin/android/platform/android-job-scheduler#using-the-android-job-scheduler

    Create a JobService

       [Service(Name = "com.xamarin.samples.downloadscheduler.DownloadJob",
            Permission = "android.permission.BIND_JOB_SERVICE")]
    class AccessServer:JobService
    {
         public override bool OnStartJob(JobParameters jobParams)
    {
        Task.Run(() =>
        {
            // Work is happening asynchronously
    
            // Have to tell the JobScheduler the work is done. 
            JobFinished(jobParams, false);
        });
    
        // Return true because of the asynchronous work
        return true;
    }
    
    public override bool OnStopJob(JobParameters jobParams)
    {
        // we don't want to reschedule the job if it is stopped or cancelled.
        return false;
    }
    

    }

    Use Job scheduler.

            public static class JobSchedulerHelpers
           {
             public static JobInfo.Builder CreateJobBuilderUsingJobId<T>(this Context context, int jobId) where T : JobService
             {
            var javaClass = Java.Lang.Class.FromType(typeof(T));
            var componentName = new ComponentName(context, javaClass);
            return new JobInfo.Builder(jobId, componentName);
             }
          }
    
            var jobBuilder = this.CreateJobBuilderUsingJobId<AccessServer>(1);
            jobBuilder.SetPeriodic(1800000);
            JobInfo jobInfo = jobBuilder.Build();
    
    Wednesday, January 8, 2020 11:36 AM
  • User388416 posted

    Appreciate for your help @LeonLu.

    Use Job scheduler.

            public static class JobSchedulerHelpers
           {
             public static JobInfo.Builder CreateJobBuilderUsingJobId<T>(this Context context, int jobId) where T : JobService
             {
            var javaClass = Java.Lang.Class.FromType(typeof(T));
            var componentName = new ComponentName(context, javaClass);
            return new JobInfo.Builder(jobId, componentName);
             }
          }
    
            var jobBuilder = this.CreateJobBuilderUsingJobId<AccessServer>(1);
            jobBuilder.SetPeriodic(1800000);
            JobInfo jobInfo = jobBuilder.Build();
    

    Regarding code above, what is the AccessServer means? What should I put actually because got error there. Furthermore, the last three line of codes seems like do not belong to the class JobSchedulerHelpers, where should I put them?

    Thursday, January 9, 2020 10:20 AM
  • User229736 posted

    @ASolute_dev I am currently facing the same issue. Have you found a solu> @LeonLu said:

    In the android side. If you want to access the server every 30 minutes, you can use the Job scheduler to achieve that. https://docs.microsoft.com/en-us/xamarin/android/platform/android-job-scheduler#using-the-android-job-scheduler

    Create a JobService

       [Service(Name = "com.xamarin.samples.downloadscheduler.DownloadJob",
            Permission = "android.permission.BIND_JOB_SERVICE")]
    class AccessServer:JobService
    {
         public override bool OnStartJob(JobParameters jobParams)
    {
        Task.Run(() =>
        {
            // Work is happening asynchronously
    
            // Have to tell the JobScheduler the work is done. 
            JobFinished(jobParams, false);
        });
    
        // Return true because of the asynchronous work
        return true;
    }
    
    public override bool OnStopJob(JobParameters jobParams)
    {
        // we don't want to reschedule the job if it is stopped or cancelled.
        return false;
    }
    

    }

    Use Job scheduler.

            public static class JobSchedulerHelpers
           {
             public static JobInfo.Builder CreateJobBuilderUsingJobId<T>(this Context context, int jobId) where T : JobService
             {
            var javaClass = Java.Lang.Class.FromType(typeof(T));
            var componentName = new ComponentName(context, javaClass);
            return new JobInfo.Builder(jobId, componentName);
             }
          }
    
            var jobBuilder = this.CreateJobBuilderUsingJobId<AccessServer>(1);
            jobBuilder.SetPeriodic(1800000);
            JobInfo jobInfo = jobBuilder.Build();
    

    I followed your steps but it still doesn't work for me. Am I missing something? Can you give us a more detailed information? Thanks.

    Sunday, May 17, 2020 2:46 PM