Insert into table when app is killed

avskmkavskmk Member ✭✭
edited April 9 in Xamarin.Android

Hi

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()
    {
        BindLocationRequest();

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

        fusedLocationProviderClient.RequestLocationUpdates(locationRequest, GetPendingIntent());
    }

    private PendingIntent GetPendingIntent()
    {
        Intent intent = new Intent(this, typeof(MyLocationService));
        intent.SetAction(MyLocationService.ACTION_PROCESS_LOCATION);
        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();
            //Table
                    Location_Log log = new Location_Log();
                    log.Date = DateTime.Now;
                    log.IsBackground = false;
                    log.Latitude = location.Latitude;
                    log.Longitude = location.Longitude;
                    try
                    {
            // when app is foreground/background
                        MainActivity.GetInstance().UpdateText(locString);
                    }
                    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
                    MainActivity.GetInstance().InsertLog(log);
                }
            }
        }
    }

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

Thanks in Advance,

Best Answer

Answers

  • AnandmauryaAnandmaurya INMember ✭✭

    @avskmk Need to implement service If you want to insert data into background. Check this...
    https://github.com/MysticMagic/ServiceAndSqlite

  • avskmkavskmk Member ✭✭

    @Anandmaurya, are you referring to

    mRunnable = new Runnable() {
            @Override
            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.