這是我的Android項目。該應用程序顯示當前位置的信息。
但是,該應用始終顯示該位置爲空。我已經搜索了很多,但仍然無法找到解決方案或無法實施解決方案。位置始終爲空
這裏是我的MainActivity代碼,
package com.learning.pranavjain.hikerswatch;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends Activity implements LocationListener {
LocationManager locationManager;
String provider;
String sLatitude;
String sLongitude;
String sAccuracy;
String sSpeed;
String sAltitude;
TextView addressTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Hiding the status bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(),false);
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.
return;
}
locationManager.requestLocationUpdates(provider,400,1,this);
location = locationManager.getLastKnownLocation(provider);
if(location==null){
Log.i("Location1","NULL1");
Toast.makeText(this,"Location not found",Toast.LENGTH_SHORT).show();
}
if(location!=null) {
onLocationChanged(location);
Log.i("Location1","Reached here");
}
}
@Override
protected void onResume() {
super.onResume();
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.
return;
}
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
Float acc = location.getAccuracy();
Float spe = location.getSpeed();
Double alt = location.getAltitude();
Log.i("Location","reached here");
TextView latitude = (TextView) findViewById(R.id.latitude);
TextView longitude = (TextView) findViewById(R.id.longitude);
TextView accuracy = (TextView) findViewById(R.id.accuracy);
TextView speed = (TextView) findViewById(R.id.speed);
TextView altitude = (TextView) findViewById(R.id.altitude);
addressTV = (TextView) findViewById(R.id.address);
sLatitude = latitude.getText().toString();
sLongitude = longitude.getText().toString();
sAccuracy = accuracy.getText().toString();
sSpeed = speed.getText().toString();
sAltitude = altitude.getText().toString();
sLatitude += Double.toString(lat);
sLongitude += Double.toString(lng);
sAccuracy += Double.toString(acc);
sSpeed += Double.toString(spe);
sAltitude += Double.toString(alt);
latitude.setText(sLatitude);
longitude.setText(sLongitude);
accuracy.setText(sAccuracy);
speed.setText(sSpeed);
altitude.setText(sAltitude);
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(lat, lng, 1);
if (listAddresses != null && listAddresses.size() > 0) {
Log.i("PlaceInfo", listAddresses.get(0).toString());
String addressHolder = "";
for (int i = 0; i <= listAddresses.get(0).getMaxAddressLineIndex(); i++) {
addressHolder += listAddresses.get(0).getAddressLine(i) + "\n";
}
addressTV.setText("Address:\n" + addressHolder);
}
} catch (IOException e) {
e.printStackTrace();
}
Log.i("Location:", String.valueOf(lat));
Log.i("Location:", lng.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
protected void onPause() {
super.onPause();
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.
return;
}
locationManager.removeUpdates(this);
}
}
這裏是我的清單代碼,
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.location.gps" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
整個項目可以在here (GitHub)找到
getLastKnownLocation可以返回null,你應該請求一個位置更新,如果是的話。 – Raghunandan
'考慮調用ActivityCompat#requestPermissions'你真的*有*要做到這一點,如果你在Android M及以上運行 – njzk2
@Raghunandan我已經做到了這一點 –