Forum Xamarin.Android

Insert into table when app is killed

avskmkavskmk Member ✭✭
edited April 2019 in Xamarin.Android


I'm very new to Xamarin, and i'm trying to develop an app that find the location for every X minutes.
I used BraodcastReceiver background service to find the location and is working fine even the app is killed.

but want to insert the logs in database when location triggered. If the app is in foreground/background able to insert data into table. But not working when app is killed.

I'm initiating the Broadcast in Oncreate method in MainActivity.cs

private void UpdateLocation()

        fusedLocationProviderClient = LocationServices.GetFusedLocationProviderClient(this);
        if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) != Android.Content.PM.Permission.Granted)

        fusedLocationProviderClient.RequestLocationUpdates(locationRequest, GetPendingIntent());

    private PendingIntent GetPendingIntent()
        Intent intent = new Intent(this, typeof(MyLocationService));
        return PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.UpdateCurrent);

And my Broadcast receiver is

 public override void OnReceive(Context context, Intent intent)
        if (intent != null)
            string action = intent.Action;
            if (action.Equals(ACTION_PROCESS_LOCATION))
                LocationResult result = LocationResult.ExtractResult(intent);
                if (result != null)
                    var location = result.LastLocation;
                    var locString = location.Longitude.ToString() + "/" + location.Latitude.ToString();
                    Location_Log log = new Location_Log();
                    log.Date = DateTime.Now;
                    log.IsBackground = false;
                    log.Latitude = location.Latitude;
                    log.Longitude = location.Longitude;
            // when app is foreground/background
                    catch (Exception ex)
            //when app is killed
                        log.IsBackground = true;
                        Toast.MakeText(context, locString, ToastLength.Long).Show();
            // Inserting to table 
            // calling method in MainActivity.cs

Am i trying to do in wrong way. Please suggest.

Thanks in Advance,

Best Answer


  • AnandmauryaAnandmaurya INMember ✭✭

    @avskmk Need to implement service If you want to insert data into background. Check this...

  • avskmkavskmk Member ✭✭

    @Anandmaurya, are you referring to

    mRunnable = new Runnable() {
            public void run() {
                MyDBHelper myDBHelper = new MyDBHelper(getApplicationContext());
                boolean isInfoAvailable = myDBHelper.isAnyInfoAvailable(getApplicationContext());
                Toast.makeText(getApplicationContext(), String.valueOf(isInfoAvailable), Toast.LENGTH_LONG).show();
                mHandler.postDelayed(mRunnable, 10 * 1000);
        mHandler.postDelayed(mRunnable, 10 * 1000);

    If yes, this may not work with C#.

Sign In or Register to comment.