此活动有2个按钮,Start和Stop。点击按钮开始时,保存
用户位置到firebase,按钮停止停止。因此,我使用LocationManager.requestLocationUpdates
将每5秒用户位置插入firebase。但这会让我的应用程序崩溃,这是我的代码
package com.example.julio.firebasemaster;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
TextView condition;
Button start;
Button stop;
DatabaseReference mDatabase;
DatabaseReference mChild;
LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference();
mChild = mDatabase.child("user");
start = (Button) findViewById(R.id.btnStart);
stop = (Button) findViewById(R.id.btnStop);
permission();
start.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("MissingPermission")
@Override
public void onClick(View view) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener);
permission();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = (new LocationListener() {
@Override
public void onLocationChanged(Location location) {
String latitude = location.getLatitude() + " ";
String longitude = location.getLongitude() + " ";
HashMap<String, String> data = new HashMap<>();
data.put("latitude", latitude);
data.put("longitude", longitude);
mChild.setValue(data);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
});
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
locationManager.removeUpdates(locationListener);
}
});
}
private void permission() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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.
return;
}
}
}
点击开始按钮
时会发生粉碎我正在使用firebase助手,因此初始化
没有问题请帮助我,我的脑袋想要爆发思考这个错误:(
答案 0 :(得分:0)
在onLocationChanged
public void onLocationChanged(Location location) {
if (location != null) {
String latitude = location.getLatitude() + " ";
String longitude = location.getLongitude() + " ";
HashMap<String, String> data = new HashMap<>();
data.put("latitude", latitude);
data.put("longitude", longitude);
mChild.setValue(data);
}
}
我希望这会对你有所帮助