使用Android API版本23并添加了启用位置清单的权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BODY_SENSORS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.GPS_PROVIDER" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
在运行时请求权限的活动代码:
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.Manifest;
public class MainActivity extends AppCompatActivity {
LocationManager lm;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
1);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} else {
}
return;
}
}
}
}
但这一行:
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
导致错误:
我没有正确设置权限?
更新:
这是更新的代码:
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
LocationManager lm;
Location location;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
int permissionCheck = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d(TAG, "location is "+location);
if(location != null) {
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.d(TAG, "longitude : " + longitude);
Log.d(TAG, "latitude : " + latitude);
}
}
}
我不再收到编译器错误,但location
设置为mull。
在设备内启用了位置。
这是访问位置的正确方法吗?
更新2:
此处检查checkSelfPermission
但location
的权限为空:
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
LocationManager lm;
Location location;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
int permissionCheck = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION);
Log.d(TAG, "permissionCheck : " + permissionCheck);
if(permissionCheck >= 0) {
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d(TAG, "location is " + location);
if (location != null) {
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.d(TAG, "longitude : " + longitude);
Log.d(TAG, "latitude : " + latitude);
}
}
}
}