如何在棒棒糖或更高版本中打开位置并获取当前位置

Posted

技术标签:

【中文标题】如何在棒棒糖或更高版本中打开位置并获取当前位置【英文标题】:How to turn on Location in lollipop or up version and get current location 【发布时间】:2017-08-06 19:16:14 【问题描述】:

我尝试在棒棒糖设备(不是模拟器)中打开 gps 位置。我编写了实时权限代码,但无法正常工作。这是我的来源:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) 
        askForGPS();
        Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
     else 
        Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
    


private void askForGPS()
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(30 * 1000);
    mLocationRequest.setFastestInterval(5 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);
    result = LocationServices.SettingsApi.checkLocationSettings(client, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() 
        @Override
        public void onResult(LocationSettingsResult result) 
            final Status status = result.getStatus();
            switch (status.getStatusCode()) 
                case LocationSettingsStatusCodes.SUCCESS:
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    try 
                        status.startResolutionForResult(MainActivity.this, GPS_SETTINGS);
                     catch (IntentSender.SendIntentException e) 
                        e.printStackTrace();

                    
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    break;
            
        
    );


private void askForPermission(String permission, Integer requestCode) 
    if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) 
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) 

            ActivityCompat.requestPermissions(MainActivity.this, new String[]permission, requestCode);

         else 

            ActivityCompat.requestPermissions(MainActivity.this, new String[]permission, requestCode);
        
     else 
        Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
    

在按钮单击中,我尝试像这样请求许可。

askForPermission(Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);

当我运行我的应用程序时,我无法显示 alertDialog 并且无法打开位置。我不知道,但显示此 Toast 消息的程序“已被授予”。 我的 askForPermission 方法无法正常工作。我的问题是如何在实时许可的情况下打开 GPS 并获取当前位置?

 public void getCurrenctLocation(Location location)

    Log.e("Current lang", location.getLatitude()+"");
    Log.e("Current long", location.getLongitude()+"");

如何以及在哪里可以使用我的 getCurrenctLocation 方法? 谢谢

【问题讨论】:

【参考方案1】:

1) 像这样修改你的askForPermission()

private void askForPermission(String permission, Integer requestCode) 
    if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) 

            ActivityCompat.requestPermissions(MainActivity.this, new String[]permission, requestCode);

     else 
           askForGPS();
    

2) 修改onRequePermissionResult() 如下:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
     switch (requestCode) 
        case LOCATION:            //You have to declare LOCATION globally. example: private int LOCATION = 1 ;

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) 
                // Permission Granted
                askForGPS();
                Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
                        
            else 
                 if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permissions[0])) 
                     Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
                   else 
                      Toast.makeText(this, "Permission denied permanently, please go to app settings to grant permission.", Toast.LENGTH_SHORT).show(); 
                  
            
            break;
    

【讨论】:

感谢您的关注。gps 开启后如何使用 getCurrenctLocation 方法检查当前位置? @tahsinRupam 您可以使用getLastLocation/位置变化监听器获取位置 你能告诉我 onRequestPermissionsResult @tahsinRupam 中的权限是什么 您在字符串中请求的权限,(例如:Manifest.permission.WRITE_EXTERNAL_STORAGE)

以上是关于如何在棒棒糖或更高版本中打开位置并获取当前位置的主要内容,如果未能解决你的问题,请参考以下文章

来自 Android OS 8 或更高版本的连续 android 位置背景

如何在 Android M 或更高版本中在运行时更改权限时防止重新创建 Activity

如何以编程方式在 Android 的 API 23 及更高版本中获取位置(纬度、经度)?

Oracle 客户端 version 8.1.7 或更高版本报错

navigator.geolocation.getCurrentPosition 在 React Native 版本(0.60)及更高版本中不起作用,如何获取位置?

在 iOS 7 或更高版本中关闭应用程序时,iOS 可以执行操作吗?