簡體   English   中英

盡管已授予權限且GPS已打開,但用戶的位置始終返回null

[英]User's location always returns null although the permission is given and GPS is turned on

我正在嘗試讓我的應用使用用戶的位置並將其保存到GeoFire,但它始終只會返回null。 我知道有很多這樣的問題,但是請相信我,我已經嘗試了很多。

似乎事情在getLastKnownLocation中,但是我不知道如何更改它以返回當前位置。

實際上,僅當用戶發布內容時,我才需要保存位置,因此無需保持跟蹤。

這是我現在擁有的整個活動的代碼

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 您需要能夠應對被交給你的位置onLocationChanged()在你的LocationListener ,並用它做什么。 現在,您只是設置一個字段,這很好,但是不會更新UI,創建Post或其他任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM