gpt4 book ai didi

android - 当我们注销时如何在android中停止位置更新

转载 作者:行者123 更新时间:2023-11-29 23:51:57 26 4
gpt4 key购买 nike

我在我的应用程序中使用经过身份验证的用户的连续位置更新。当我从我的应用程序注销时,它崩溃了。我意识到应用程序因位置更新而崩溃,所以我想在没有经过身份验证的用户时使用 locmngr.removeUpdates(this) 删除位置更新。当用户单击 logout mAath.getUid() 时,它变为 null,我得到的错误是 Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on空对象引用

try {
locmngr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 7, 30, new LocationListener() {
@Override
public void onLocationChanged(final Location location) {

// Add a marker at your current location and move the camera
Log.e("location", location.toString());
Log.e("location", "updating");
// Toast.makeText(ProfileNavigation.this, "Location : "+location.getLatitude()+" , "+location.getLongitude(), Toast.LENGTH_SHORT).show();
//double a = location.getLongitude();
//double b = location.getLatitude();
if(!mAuth.getUid().isEmpty()) {

db.collection("UserProfile").document(mAuth.getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {

Log.e("maps", "updating");
Map<String, Object> data = new HashMap<>();
data.put("Latitude", location.getLatitude());
data.put("Longitude", location.getLongitude());

db.collection("UserProfile").document(mAuth.getUid())
.set(data, SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.e("maps", "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("maps", "Error writing document", e);
}
});
} else {

Log.e("firestore", "Error getting documents.", task.getException());
}
} else {

Log.e("firestore", "Error getting documents.", task.getException());
}
}
});

}
else {
locmngr.removeUpdates(this);
}
}
}
}

最佳答案

onPause() 方法中调用removeUpdates 方法。

@Override
protected void onPause() {
super.onPause();
locmngr.removeUpdates(this);
Log.i(TAG, "onPause, done");
}

你还得到:

Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()

if(logout mAath.getUid()!=null){
//do operation here
}

希望对你有所帮助!!

关于android - 当我们注销时如何在android中停止位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50830291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com