我是Android开发的新手,因此如果这是一个愚蠢的问题,请提前道歉。
我正在运行一个简单的应用程序,该应用程序可以跟踪用户的位置并将其存储在实时数据库中。
该应用程序在仿真器上可以完美运行,但不适用于真实设备。
它变得很慢,并显示一条消息,提示“ V / FA:不活动,正在与服务断开连接”,并且当它开始运行时,没有任何内容写入数据库。
这是我的清单:
<?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:
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的快照:
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
同样,这在模拟器上也可以正常运行,运行并存储到数据库。 但是在实际设备上失败。
谢谢。
答案 0 :(得分:-1)
请尝试执行以下步骤。本文对此原因进行了说明:V/FA: Inactivity, disconnecting from the service
步骤:
1)从您的手机/模拟器中卸载应用。
2)然后转到android studio主菜单栏中的“文件”选项。
3)然后单击Invalidatecasha / restart。