'unable to update the FireBase database from a service

My service is running in the background. I am getting the toast notification of the latitude and longitude but my database is not getting updated.

In the onCreate() function I am calling the getlocation() function

getlocation function is used to give the location toast and also update the firebase database

I am getting the toast notification but the database is not getting updated

public class BackgroundService extends Service {

    FusedLocationProviderClient mfusedLocationClient;
    DatabaseReference rootRef,demoRef;
    updatedatabase up;

    public Context context = this;
    public Handler handler = null;
    public static Runnable runnable = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        mfusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        rootRef= FirebaseDatabase.getInstance().getReference();
        demoRef=rootRef.child("demo");
         up=new updatedatabase();
        Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();

        handler = new Handler();
        runnable = new Runnable() {
            public void run() {
                getlocation();

                Toast.makeText(context, "Service is still running", Toast.LENGTH_LONG).show();
                handler.postDelayed(runnable, 10000);
            }
        };

        handler.postDelayed(runnable, 15000);
    }
    void getlocation() {


        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.

        }
        mfusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                if(task.isSuccessful())
                {
                    Location location=task.getResult();
                    GeoPoint geoPoint=new GeoPoint(location.getLatitude(),location.getLongitude());
                    String str=geoPoint.getLatitude()+" "+geoPoint.getLongitude();
                    demoRef.setValue(str);
                    Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();

                }


            }
        });



    }

    @Override
    public void onDestroy() {
        /* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
        //handler.removeCallbacks(runnable);
        Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source