java - 尽管授予了权限并且打开了 GPS,但用户的位置始终返回 null

标签 java android gps location geofire

我试图让我的应用程序获取用户的位置并将其保存到 GeoFire,但它总是返回 null。我知道有很多这样的问题,但相信我,我已经尝试了很多。

看起来这个东西在 getLastKnownLocation 中,但我不知道如何更改它以返回当前位置。

实际上,仅当用户发布内容时我才需要保存位置,因此无需持续跟踪它。

这是我现在拥有的整个 Activity 的代码

public class GetHelpActivity extends AppCompatActivity {
Button postBT;
EditText editHeading;
EditText editInfo;
EditText editAddress;
Location mLocation;
protected Context context;
double latitude;
double longitude;
Post post;
DatabaseReference myDB;
FirebaseUser currentUser;
FirebaseAuth mAuth;
String postID;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_help);

    postBT = findViewById(R.id.postBT);
    editHeading = findViewById(R.id.editHeading);
    editInfo = findViewById(R.id.editInfo);
    editAddress = findViewById(R.id.editAddress);

    final LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            mLocation = location;
            Log.d("Location Changes", location.toString());
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d("Status Changed", String.valueOf(status));
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d("Provider Enabled", provider);
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d("Provider Disabled", provider);
        }
    };

    final Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);

    final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    final Looper looper = null;


    myDB = FirebaseDatabase.getInstance().getReference();
    final GeoFire geoFire = new GeoFire(myDB.child("postLocation"));

    postID = myDB.child("posts").push().getKey();

    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();

    final String userID = currentUser.getUid();

    postBT.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String heading = editHeading.getText().toString();
            String info = editInfo.getText().toString();
            String address = editAddress.getText().toString();


            if (ActivityCompat.checkSelfPermission(GetHelpActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(GetHelpActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                return;
            }

            locationManager.requestSingleUpdate(criteria, locationListener, looper);
            mLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (mLocation == null) {
                Toast.makeText(GetHelpActivity.this, "Location is null", Toast.LENGTH_LONG).show();
                return;
            }

            if (valid(heading, info, address)) {

                post = new Post(userID, heading, info, address, latitude, longitude);

                latitude = mLocation.getLatitude();
                longitude = mLocation.getLongitude();

                myDB.child("posts").push().setValue(post);

                String key = geoFire.getDatabaseReference().push().getKey();

                geoFire.setLocation(key, new GeoLocation(latitude, longitude));
            }
        }
    });
}

private boolean valid(String heading, String info, String address) {
    if (heading.length() == 0) {
        Toast.makeText(GetHelpActivity.this, "Please, fill in the heading of your post", Toast.LENGTH_LONG).show();
        return false;
    }
    if (heading.length() > 25) {
        Toast.makeText(GetHelpActivity.this, "Your heading should be shorter (up to 25 characters)", Toast.LENGTH_LONG).show();
        return false;
    }
    if (info.length() == 0) {
        Toast.makeText(GetHelpActivity.this, "Please, fill in the info about your post, so volunteers will be able to understand it clearly", Toast.LENGTH_LONG).show();
        return false;
    }
    if (info.length() > 300) {
        Toast.makeText(GetHelpActivity.this, "Post's info should be no longer than 300 characters", Toast.LENGTH_LONG).show();
        return false;
    }
    if (address.length() == 0) {
        Toast.makeText(GetHelpActivity.this, "Please, fill in the address, so volunteer will be able to find you quickly", Toast.LENGTH_LONG).show();
        return false;
    }
    return true;
}

}

最佳答案

getLastLocation() 经常返回 null。您需要对 LocationListener 中传递给 onLocationChanged() 的位置使用react,并对其执行某些操作。现在,您只需设置一个字段,这很好,但不会更新您的 UI、创建您的 Post 或其他任何内容。

关于java - 尽管授予了权限并且打开了 GPS,但用户的位置始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50011478/

相关文章:

android - 最新的 Google 应用内结算版本是什么?

java - 我正在恢复一个数据库,我需要在我的 Java 应用程序中锁定它的任何 Activity

java - 检索项目 : No resource found that matches the given name android 的父级时出错

android - 在 Canvas 上绘制部分图像

c++ - 从小工具读取 GPS 数据

iphone - Xcode:如何显示 GPS 强度值?

java - 向 Runtime.getRuntime() 添加参数?

java - 手动计算字符串 compareTo 字符串的输出

java - 组织.postgresql.util.PSQLException : ERROR: relation "app_user" does not exist

android - 当前位置 GPS,程序意外关闭