Firebase数据库未更新且没有错误,响应缓慢 [英] Firebase database is not updating with no errors, slow response

查看:62
本文介绍了Firebase数据库未更新且没有错误,响应缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手,因此如果这是一个愚蠢的问题,请提前道歉.

I'm a newbie to Android development, so apologies in advance if this is a stupid question.

我正在运行一个简单的应用程序,该应用程序可以跟踪用户的位置并将其存储在实时数据库中.

I'm running a simple app that tracks the user's location and store it in a real-time db.

该应用程序在仿真器上可以正常运行,但不适用于真实设备.

the app works perfectly fine on the emulator, but not for the real devices.

它变得很慢,并显示一条消息,提示"V/FA:不活动,正在与服务断开连接",并且当它开始运行时,没有任何内容写入数据库.

it gets slow with a message showing that "V/FA: Inactivity, disconnecting from service", and when it starts running, nothing is written to the database.

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alice.locationfinder3">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

和我的build.gradle:

and my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.alice.locationfinder3"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.google.firebase:firebase-messaging:12.0.1'
    implementation 'com.google.firebase:firebase-database:12.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.android.gms:play-services:12.0.1'
}

apply plugin: 'com.google.gms.google-services'

最后,我的主要活动:

package com.example.alice.locationfinder3;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.location.Criteria;


import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.text.SimpleDateFormat;
import java.util.Date;

import static android.location.Criteria.ACCURACY_FINE;

public class MainActivity extends AppCompatActivity {

    private LocationManager locationManager;
    private LocationListener locationListener;


    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference mDatabase = database.getReference();

    double latitude; // latitude
    double longitude; // longitude

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(ACCURACY_FINE);
        String bestProvider = locationManager.getBestProvider(criteria, true);


        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.d("Location: ", location.toString());

                latitude = location.getLatitude();
                longitude = location.getLongitude();


                long time = location.getTime();
                Date date = new Date(time);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String timestamp = sdf.format(date);

                writeNewPoint(timestamp, longitude, latitude);



            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };


        if(Build.VERSION.SDK_INT < 23)
        {
            locationManager.requestLocationUpdates(bestProvider, 0, 0, locationListener);
        }

        else {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);

            }
            else {
                locationManager.requestLocationUpdates(bestProvider, 0, 0, locationListener);
            }
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        Criteria criteria;
        criteria = new Criteria();
        criteria.setAccuracy(ACCURACY_FINE);
        String bestProvider = locationManager.getBestProvider(criteria, true);
        if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED)
        {
            if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
            {
                locationManager.requestLocationUpdates(bestProvider, 0, 0, locationListener);
            }
        }
    }


    private void writeNewPoint(String timestamp, double longitude, double latitude) {
        Point point = new Point(longitude, latitude);

        mDatabase.child("points").child(timestamp).setValue(point);
    }




}

logcat的快照:

Snapshot from logcat:

09-02 16:05:11.150 28873-28906/com.example.alice.locationfinder3 V/FA: Inactivity, disconnecting from the service
09-02 16:05:11.170 28873-28958/com.example.alice.locationfinder3 I/FirebaseCrash: Sending crashes
09-02 16:05:31.195 28873-28873/com.example.alice.locationfinder3 D/Location:: Location[gps XX.7591,XX.6441 hAcc=64 et=+16h53m5s212ms alt=604.5306458863317 vel=0.14499298 bear=124.898796 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
0

同样,这在仿真器上可以正常运行,运行并存储到数据库. 但是在实际设备上失败了.

Again, this works fine with the emulator, runs and stores to the db. But it fails with real devices.

谢谢.

推荐答案

尝试执行以下步骤.本文中说明了原因:

Try following these steps.The reasoning is explained in this post: V/FA: Inactivity, disconnecting from the service

步骤:

1)从您的手机/模拟器中卸载应用程序.

1)Uninstall the app from your mobile/emulator.

2)然后转到android studio主菜单栏中的文件"选项.

2)Then go to the File option in the main menubar in the android studio.

3)然后单击Invalidatecasha/restart.

3)Then click on Invalidatecasha/restart.

这篇关于Firebase数据库未更新且没有错误,响应缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆