我的应用程序没有成功获取当前位置
My app doesn't get the current location successfully
我是 android 编程的新手。我想获取当前用户位置并显示 he/she 附近的餐厅。所以我写了下面的代码来获取 android 上的位置 studio.But 它没有成功。我还在我的项目中使用了一个 volley 库来从 wordpress 获取 json 信息。不知道是volley库干扰位置还是not.My代码是:
private void getlocationaddresss(){
progressBar.setVisibility(View.VISIBLE);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener=new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Geocoder geocoder = new Geocoder(MainActivity.this, new Locale("fa"));
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
cityName = addresses.get(0).getLocality();
stateName=addresses.get(0).getAdminArea();
progressBar.setVisibility(View.GONE);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this,"لطفا لوکیشن خود را روشن نمایید",Toast.LENGTH_LONG).show();
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
};
configure_button();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 10:
configure_button();
break;
default:
break;
}
}
此方法用于检查权限并调用requestLocationUpdates。
void configure_button() {
// first check for permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
,10);
}
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, listener);
}
我该如何修复它?
您应该检查用户是否打开了位置以获取当前位置。
以下是您可以直接在应用中使用的有用方法。
您可以在 activity 中这样检查。其中 this 指的是当前 activity 上下文。
if (!isLocationEnabled(this)){
showLocationSettingsAlert(this);
return;
}
和
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
如果未开启,则开始位置设置 activity
public static void showLocationSettingsAlert(final Context mContext) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
//Setting Dialog Title
alertDialog.setTitle("Enable GPS");
//Setting Dialog Message
alertDialog.setMessage("GPS permission allows us to access location. Please allow in App Settings for additional functionality.");
//On Pressing Setting button
alertDialog.setPositiveButton(R.string.action_settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
//On pressing cancel button
alertDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
我是 android 编程的新手。我想获取当前用户位置并显示 he/she 附近的餐厅。所以我写了下面的代码来获取 android 上的位置 studio.But 它没有成功。我还在我的项目中使用了一个 volley 库来从 wordpress 获取 json 信息。不知道是volley库干扰位置还是not.My代码是:
private void getlocationaddresss(){
progressBar.setVisibility(View.VISIBLE);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener=new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Geocoder geocoder = new Geocoder(MainActivity.this, new Locale("fa"));
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
cityName = addresses.get(0).getLocality();
stateName=addresses.get(0).getAdminArea();
progressBar.setVisibility(View.GONE);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this,"لطفا لوکیشن خود را روشن نمایید",Toast.LENGTH_LONG).show();
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
};
configure_button();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 10:
configure_button();
break;
default:
break;
}
}
此方法用于检查权限并调用requestLocationUpdates。
void configure_button() {
// first check for permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
,10);
}
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, listener);
}
我该如何修复它?
您应该检查用户是否打开了位置以获取当前位置。 以下是您可以直接在应用中使用的有用方法。
您可以在 activity 中这样检查。其中 this 指的是当前 activity 上下文。
if (!isLocationEnabled(this)){
showLocationSettingsAlert(this);
return;
}
和
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
如果未开启,则开始位置设置 activity
public static void showLocationSettingsAlert(final Context mContext) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
//Setting Dialog Title
alertDialog.setTitle("Enable GPS");
//Setting Dialog Message
alertDialog.setMessage("GPS permission allows us to access location. Please allow in App Settings for additional functionality.");
//On Pressing Setting button
alertDialog.setPositiveButton(R.string.action_settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
//On pressing cancel button
alertDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}