无法从服务更新 FireBase 数据库

发布于 2025-01-20 08:03:24 字数 3181 浏览 0 评论 0原文

我的服务在后台运行。我收到了纬度和经度的敬酒通知,但我的数据库没有更新。

在onCreate()函数中,我正在调用getLocation()函数

getLocation函数用于给出位置吐司,并且更新firebase数据库,

我收到的吐司通知,但数据库没有更新

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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文