这是我的代码:
// fungsi ngecek lokasi GPS device pengguna
private void lokasi(){
location = locationManager.getLastKnownLocation(provider);
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;
}
// permintaan update lokasi device dalam waktu 10 detik
locationManager.requestLocationUpdates(provider, 10000, 1, this);
if(location!=null){
onLocationChanged(location);
callListVolley(latitude, longitude);
}else{
Toast.makeText(this, "Lokasi device pengguna tidak ditemukan.\nMohon hidupkan GPS.",Toast.LENGTH_LONG).show();
swipe.setRefreshing(false);
}
}
我还在清单中添加了android.permission.ACCESS_COARSE_LOCATION和android.permission.ACCESS_FINE_LOCATION
答案 0 :(得分:0)
我有一些问题。我解决它就像轰鸣声
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
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;
}
// your codes
}
用户需要允许应用程序的位置权限从API级别23开始运行。