无法在后台服务中将数据发送到Firebase

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在后台服务中将数据发送到Firebase相关的知识,希望对你有一定的参考价值。

将数据发送到Firebase但无法发送,我的服务崩溃但应用程序仍在运行,当我删除Firebase代码时,服务正常运行。

我只想用当前位置将数据上传到Firebase。每件事情都很完美,但Firebase不会将数据发送到数据库,但它可以在活动中使用相同的代码。

开始服务,

public class location_service extends Service
{
    private static final String TAG = "BOOMBOOMTESTGPS";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 50000;
    private static final float LOCATION_DISTANCE = 10f;
    DatabaseReference databasecustomer;
    private class LocationListener implements android.location.LocationListener
    {
        Location mLastLocation;


        public LocationListener(String provider)
        {
            Log.e(TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        }

        @Override
        public void onLocationChanged(Location location)
        {

            Log.e("onLocationChanged: " ,"");
            mLastLocation.set(location);
            String lats= "s";
            String lngs= "s";
            databasecustomer= FirebaseDatabase.getInstance().getReference("Labor");
            String id= databasecustomer.push().getKey();
            send_request send_request=new send_request(id,lats,lngs);
            databasecustomer.child(id).setValue(send_request);
            Toast.makeText(getApplicationContext(),"This is my toast message"+id,Toast.LENGTH_LONG).show();
        }

        @Override
        public void onProviderDisabled(String provider)
        {
            Log.e(TAG, "onProviderDisabled: " + provider);
        }

        @Override
        public void onProviderEnabled(String provider)
        {
            Log.e(TAG, "onProviderEnabled: " + provider);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
            Log.e(TAG, "onStatusChanged: " + provider);
        }
    }

    LocationListener[] mLocationListeners = new LocationListener[] {
            new LocationListener(LocationManager.GPS_PROVIDER),
            new LocationListener(LocationManager.NETWORK_PROVIDER)
    };

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onCreate()
    {
        Log.e(TAG, "onCreate");
        initializeLocationManager();
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[1]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "network provider does not exist, " + ex.getMessage());
        }
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[0]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }
    }

    @Override
    public void onDestroy()
    {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        if (mLocationManager != null) {
            for (int i = 0; i < mLocationListeners.length; i++) {
                try {
                    mLocationManager.removeUpdates(mLocationListeners[i]);
                } catch (Exception ex) {
                    Log.i(TAG, "fail to remove location listners, ignore", ex);
                }
            }
        }
    }

    private void initializeLocationManager() {
        Log.e(TAG, "initializeLocationManager");
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }
}
答案

可能是因为firebase中数据库的安全性,将数据库的规则更改为

{
  "rules": {
    ".read": true,
    ".write": true
  }
}
  • 确保你的android工作室连接到firebase和firebase数据库

以上是关于无法在后台服务中将数据发送到Firebase的主要内容,如果未能解决你的问题,请参考以下文章

Firebase Fabric Crashlytics 未将数据发送到服务器

无法在 setTimeout 中将流发送到服务器

在 Python 中将 ENTER 按键发送到没有 gui 的后台 exe

Android:从运行时生成的文本字段中将数据保存到Firebase

无法将数据从 Stripe webhook 发送到 Firebase

在 Flutter 中将数据保存到 Firebase 实时数据库