Я разрабатываю приложение, которое отслеживает местоположение устройства и записывает это местоположение в firebase, приложение успешно подключилось к серверу firebase, но нет данных о местоположении устройства. кто-нибудь может найти мне решение для этого? и как просмотреть сохраненные данные в firebase. ниже код, который я использовал в своем проекте
TrackingService. java:
public class TrackingService extends Service {
private static final String TAG = TrackingService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startForeground();
else
startForeground(1, new Notification());
loginToFirebase();
}
//Create the persistent notification//
private void startForeground() {
String NOTIFICATION_CHANNEL_ID = "com.example.location2";
String channelName = "location2";
String stop = "stop";
registerReceiver(stopReceiver, new IntentFilter(stop));
PendingIntent broadcastIntent = PendingIntent.getBroadcast(
this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
//.setSmallIcon(R.drawable.icon_1)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.tracking_enabled_notif))
.setOngoing(true)
.setContentIntent(broadcastIntent)
.setSmallIcon(R.drawable.tracking_enabled)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
protected BroadcastReceiver stopReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Unregister the BroadcastReceiver when the notification is tapped//
unregisterReceiver(stopReceiver);
//Stop the Service//
stopSelf();
}
};
private void loginToFirebase() {
//Authenticate with Firebase, using the email and password we created earlier//
String email = getString(R.string.test_email);
String password = getString(R.string.test_password);
//Call OnCompleteListener if the user is signed in successfully//
FirebaseAuth.getInstance().signInWithEmailAndPassword(
email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
//If the user has been authenticated...//
if (task.isSuccessful()) {
//...then call requestLocationUpdates//
requestLocationUpdates();
} else {
//If sign in fails, then log the error//
Log.d(TAG, "Firebase authentication failed");
}
}
});
}
//Initiate the request to track the device's location//
private void requestLocationUpdates() {
LocationRequest request = new LocationRequest();
//Specify how often your app should request the device’s location//
request.setInterval(10000);
//Get the most accurate location data available//
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(this);
final String path = getString(R.string.firebase_path);
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
//If the app currently has access to the location permission...//
if (permission == PackageManager.PERMISSION_GRANTED) {
//...then request location updates//
client.requestLocationUpdates(request, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
//Get a reference to the database, so your app can perform read and write operations//
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
Location location = locationResult.getLastLocation();
if (location != null) {
//Save the location data to the database//
ref.setValue(location);
}
}
}, null);
}
}
}