Arvind Chourasiya
Arvind Chourasiya

Reputation: 17422

Continue execution of services in background while using app Xamarin Android

I have some data to insert in database when user login mobile app. As well as I have some other services calls too, which makes my app login worst. So here I want to know that how can I login immediately & continue execution of other data services in background.

However I am making async call which is doesn't seem to work. The service calls I am making is it running in background? or I can make it more batter

private async Task InsertPushKeys()
{       
    foreach (var item in SISConst.Mychildren)
    {
        pushInfo.OrganizationId = Convert.ToInt64(item.OrganizationID);
        pushInfo.OsType = "Android";
        pushInfo.ServerkeyPush = SISConst.LmgKey;
        var ignore = await logDAL.InsertPushInfo(pushInfo);
    }
}

Below line executing the service.

var ignore = await logDAL.InsertPushInfo(pushInfo);

Edit 1: These service I am calling inside login button in the same order

_btnSignUp.Click += async (s, e) =>
                    {
var loggedInUser = await uLogin.UserLogin(_inputName.Text.Trim(), _inputPassword.Text);
Task.Run(async () => { mychildList = await uLogin.BindMychildrenGridData(result.UserID); }).Wait(); 
await DeletePushKeys(loggedInUser.UserID);
InsertPushKeys();
                    };

Upvotes: 1

Views: 899

Answers (3)

Abhinav Gupta
Abhinav Gupta

Reputation: 31

Android provides JobService Class where you can schedule JobService from JobScheduler Class. JobScheduler runs the service in the background and you can go ahead with logging in. You can find one Xamarin example in here: https://blog.xamarin.com/replacing-services-jobs-android-oreo-8-0/

Create a JobService class and add your InsertPushKeys code inside OnStartJob function. Make sure to start it on New thread.

OnStartJob(JobParameters parameters){
 new Thread(new Runnable() {
       public void run() {
           await InsertPushKeys();
       }
   }).start();
}

Now Build the Job using JobInfo Class as shown in above link:

ComponentName componentName = new ComponentName(this, MyJobService.class);
JobInfo jobInfo = new JobInfo.Builder(12, componentName)
       .setRequiresCharging(true)
   .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
   .build();

At last, you can schedule your job and it will get executed by android.

JobScheduler jobScheduler = (JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = jobScheduler.schedule(jobInfo);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
   Console.WriteLine(TAG, "Job scheduled!");
} else {
   Console.WriteLine(TAG, "Job not scheduled");
}

Make sure to run the task on a different thread.

Upvotes: 2

Arvind Chourasiya
Arvind Chourasiya

Reputation: 17422

Call your services inside Task.Run without using await keyword. Await is holding control until it complete total execution. It might give warning for you that use await keyword but you can ignore it.

Task.Run(async () =>
{       
     InsertPushKeys();
});

& don't try using .Wait()(until you need it) at the end of method it is holding your execution.

Upvotes: 1

entropy.maximum
entropy.maximum

Reputation: 477

This is what you're looking for. Services are a construct provided by the Android framework to do exactly what you need. For more info, check -

http://www.vogella.com/tutorials/AndroidServices/article.html

TLDR : Put your API calls inside the service's onCreate(), and return START_STICKY from its onStartCommand(). Do not forget to call startService() on an instance of the service.

Upvotes: 1

Related Questions