Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的应用程序没有';无法成功获取当前位置_Java_Android_Geolocation - Fatal编程技术网

Java 我的应用程序没有';无法成功获取当前位置

Java 我的应用程序没有';无法成功获取当前位置,java,android,geolocation,Java,Android,Geolocation,我是一名android编程新手。我想为他/她获取当前用户位置并在餐厅附近显示。因此,我在android studio上为获取位置编写了以下代码。但没有成功。此外,我在项目中使用了一个截取库,从wordpress获取json信息。我不知道截击库是否干扰了位置。我的代码是: private void getlocationaddresss(){ progressBar.setVisibility(View.VISIBLE); locationManager = (Locat

我是一名android编程新手。我想为他/她获取当前用户位置并在餐厅附近显示。因此,我在android studio上为获取位置编写了以下代码。但没有成功。此外,我在项目中使用了一个截取库,从wordpress获取json信息。我不知道截击库是否干扰了位置。我的代码是:

  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;
    }
}

如何修复它?

您应该检查用户是否已打开位置以获取当前位置。 以下是一些有用的方法,您可以直接在应用程序中使用

您可以在活动中这样检查。这里指的是当前活动上下文

 if (!isLocationEnabled(this)){
                showLocationSettingsAlert(this);
                return;
            }

如果未启用,则启动位置设置活动

 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();
    }

大概有吧!请张贴您的日志!因为代码似乎很好,所以您必须确保设备中的GPS已启用,以获取更新用户位置。@Rizwanatta在logcat中没有任何错误。仅侦听器是empty@Navin我确定它是启用的,但我认为它的侦听器是空的!您的代码似乎一切正常!您尝试在哪个方法中使用此方法getlocationaddresss()????
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);
    }
}
 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();
    }