新权限系统(位置整合)
New Permission System (Integration of Location)
这是我的第一个问题。我学习计算机科学,正在努力提高 android 开发技能。因此我想制作一个简单的天气应用程序。我不习惯android 6.0的新权限系统,所以我需要你的帮助。
我总是遇到问题,日志告诉我找不到位置(我使用的字符串,如果位置为空)。我正在这样做,因为我想检查是否有给定的位置。之后我想将纬度和高度传递给 API 以请求坐标的天气数据。请检查我的代码并尝试帮助我并给我一些改进代码的建议:
public class MainActivity extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
Location location;
String provider;
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//LocationManager Setup get System Services
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
//Get Last known Location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, permissions, 10);
} else {
locationManager.requestLocationUpdates(provider, 400, 1000, this);
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.i("Location Info", "Location Achieved");
} else {
Log.i("Location Info", "No Location found");
}
}
}
@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) {
ActivityCompat.requestPermissions(this, permissions, 10);
return;
} else {
locationManager.requestLocationUpdates(provider, 400, 1000, this);
}
}
@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) {
ActivityCompat.requestPermissions(this, permissions, 10);
return;
} else {
locationManager.removeUpdates(this);
}
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
Log.i("Latitude", lat.toString());
Log.i("Longitude", lng.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
你需要处理Permissions Result
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: { // for your case 10
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
这是我的第一个问题。我学习计算机科学,正在努力提高 android 开发技能。因此我想制作一个简单的天气应用程序。我不习惯android 6.0的新权限系统,所以我需要你的帮助。
我总是遇到问题,日志告诉我找不到位置(我使用的字符串,如果位置为空)。我正在这样做,因为我想检查是否有给定的位置。之后我想将纬度和高度传递给 API 以请求坐标的天气数据。请检查我的代码并尝试帮助我并给我一些改进代码的建议:
public class MainActivity extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
Location location;
String provider;
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//LocationManager Setup get System Services
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
//Get Last known Location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, permissions, 10);
} else {
locationManager.requestLocationUpdates(provider, 400, 1000, this);
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.i("Location Info", "Location Achieved");
} else {
Log.i("Location Info", "No Location found");
}
}
}
@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) {
ActivityCompat.requestPermissions(this, permissions, 10);
return;
} else {
locationManager.requestLocationUpdates(provider, 400, 1000, this);
}
}
@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) {
ActivityCompat.requestPermissions(this, permissions, 10);
return;
} else {
locationManager.removeUpdates(this);
}
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
Log.i("Latitude", lat.toString());
Log.i("Longitude", lng.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
你需要处理Permissions Result
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: { // for your case 10
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}