我正在尝试检索设备的位置,但由于某种原因该位置没有收到。有人可以检查我的代码并让我知道代码中的错误是什么。谢谢你。
主要活动
public class MainActivity extends AppCompatActivity implements LocationListener {
LocationManager locManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//get a reference to the location services to initialize the location manager object
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
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;
}
// Get name of the provider in this case which would be gps
// String provider = locManager.getBestProvider(criteria, false);
// if (provider != null && !provider.equals("")) {
// Get the location
// Location location = locManager.getLastKnownLocation(provider);
Location location = getLastKnownLocation();
private Location getLastKnownLocation() {
LocationManager mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
//set frequency of location update parameters
locManager.requestLocationUpdates(provider, 120000, 10, this);
if (location != null)
onLocationChanged(location);
else {
TextView tvLongitude = (TextView) findViewById(R.id.curr_longitude);
tvLongitude.setText("Location can't be retrieved");
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
}
/* } else {
//create a toast indicating error.
Toast.makeText(getBaseContext(), "OOPS something went wrong", Toast.LENGTH_SHORT).show();
}*/
}
public void onLocationChanged(Location location) {
// text view for company caption
TextView jackFruit = (TextView) findViewById(R.id.jack_fruit);
// Get TextView curr_longitude
TextView tvLongitude = (TextView) findViewById(R.id.curr_longitude);
// Getting TextView curr_latitude
TextView tvLatitude = (TextView) findViewById(R.id.curr_latitude);
// setting company caption
jackFruit.setText("Jackfruit Systems: My current location");
// Setting Current Longitude in the TextView
tvLongitude.setText("Longitude:" + location.getLongitude());
// Setting Current Latitude in the TextView
tvLatitude.setText("Latitude:" + location.getLatitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jackfruit.driverlocationinfo">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>