无法从服务更新 FireBase 数据库
我的服务在后台运行。我收到了纬度和经度的敬酒通知,但我的数据库没有更新。
在onCreate()函数中,我正在调用getLocation()函数
getLocation函数用于给出位置吐司,并且更新firebase数据库,
我收到的吐司通知,但数据库没有更新
public class BackgroundService extends Service {
FusedLocationProviderClient mfusedLocationClient;
DatabaseReference rootRef,demoRef;
updatedatabase up;
public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
mfusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
rootRef= FirebaseDatabase.getInstance().getReference();
demoRef=rootRef.child("demo");
up=new updatedatabase();
Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();
handler = new Handler();
runnable = new Runnable() {
public void run() {
getlocation();
Toast.makeText(context, "Service is still running", Toast.LENGTH_LONG).show();
handler.postDelayed(runnable, 10000);
}
};
handler.postDelayed(runnable, 15000);
}
void getlocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
}
mfusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if(task.isSuccessful())
{
Location location=task.getResult();
GeoPoint geoPoint=new GeoPoint(location.getLatitude(),location.getLongitude());
String str=geoPoint.getLatitude()+" "+geoPoint.getLongitude();
demoRef.setValue(str);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onDestroy() {
/* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
//handler.removeCallbacks(runnable);
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
}
}
My service is running in the background. I am getting the toast notification of the latitude and longitude but my database is not getting updated.
In the onCreate() function I am calling the getlocation() function
getlocation function is used to give the location toast and also update the firebase database
I am getting the toast notification but the database is not getting updated
public class BackgroundService extends Service {
FusedLocationProviderClient mfusedLocationClient;
DatabaseReference rootRef,demoRef;
updatedatabase up;
public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
mfusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
rootRef= FirebaseDatabase.getInstance().getReference();
demoRef=rootRef.child("demo");
up=new updatedatabase();
Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();
handler = new Handler();
runnable = new Runnable() {
public void run() {
getlocation();
Toast.makeText(context, "Service is still running", Toast.LENGTH_LONG).show();
handler.postDelayed(runnable, 10000);
}
};
handler.postDelayed(runnable, 15000);
}
void getlocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
}
mfusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if(task.isSuccessful())
{
Location location=task.getResult();
GeoPoint geoPoint=new GeoPoint(location.getLatitude(),location.getLongitude());
String str=geoPoint.getLatitude()+" "+geoPoint.getLongitude();
demoRef.setValue(str);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onDestroy() {
/* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
//handler.removeCallbacks(runnable);
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论