2016-10-08 27 views
4

我可以编写获取用户位置的代码,并且在API < 23中可以正常工作。但在API 23及更高版本中,我的代码在Log中没有返回任何内容。获取用户在android API中的位置23

更多详细信息:
我手动启用设备的GPS。 在第一次运行应用程序请求权限并且没有日志返回。
在接下来的运行应用程序返回我准备的吐司(检查你的提供者)。

这是我写的代码:

public class MainActivity extends AppCompatActivity implements LocationListener { 

private static final int MY_PERMISSIONS_REQUEST_COARSE_LOCATION = 124; 
private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 123; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Here, thisActivity is the current activity 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 

     } else { 

      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_FINE_LOCATION); 
     } 
    } 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_COARSE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_COARSE_LOCATION)) { 

     } else { 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_COARSE_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; 
    } 
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    String provider = locationManager.getBestProvider(new Criteria(), false); 
    Location location = locationManager.getLastKnownLocation(provider); 
    if (location == null) { 
     Toast.makeText(this, "Check your provider", Toast.LENGTH_SHORT).show(); 
    } else { 
     Log.i("New Location", "lat: " + location.getLatitude()); 
     Log.i("New Location", "lng: " + location.getLongitude()); 
    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.i("Location", "lat: " + location.getLatitude()); 
    Log.i("Location", "lng: " + location.getLongitude()); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

}

+0

在您的getBestProvider()方法中,您只检查启用的提供程序,如果提供程序未启用,该位置将始终返回** null **。 在这种情况下,您需要检查提供程序是否已启用,如果已禁用,请让用户启用它。 – AndroidHacker

+0

我改为真,但没有解决。如何做第二个?我几乎是新的android。 –

回答

5

在Android的API 23+有处理视为 “危险的” 权限的新途径,that's的android.permission.ACCESS_FINE_LOCATIONandroid.permission.ACCESS_COARSE_LOCATION的情况下等等。如果您已经使用android.permission.ACCESS_FINE_LOCATION,则不需要处理android.permission.ACCESS_COARSE_LOCATION

请从官方文档中查看此链接,该文档向您展示了如何在Android API 23+的运行时处理权限。

https://developer.android.com/training/permissions/requesting.html

这其中也可能会帮助你。

https://developer.android.com/guide/topics/location/strategies.html

如何检查权限:

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, 
    Manifest.permission.ACCESS_FINE_LOCATION); 

如何请求权限:

if (ContextCompat.checkSelfPermission(thisActivity, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
    != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.ACCESS_FINE_LOCATION)) { 

     // Show an expanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
      MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 

     // MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

请注意,即使你在运行时请求权限,你仍然必须保持Android清单中的权限条目如下:

<manifest ... > 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    ... 
    <!-- Needed only if your app targets Android 5.0 (API level 21) or higher. --> 
    <uses-feature android:name="android.hardware.location.gps" /> 
    ... 
</manifest> 

在检查并授予权限后,您应该查看上面位置策略的链接,您可以在其中找到缺少的详细信息。

基本上你没有注册侦听器来接收位置更新。在onCreate()方法末尾添加此行后,您应该开始在日志中查看位置更新。

// Register the listener with the Location Manager to receive location updates 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
+0

没有解决问题。我认为问题不在许可中。因为再次回到Toast。当Toast变成“location == null”时。 –

+0

请检查更新的答案。你现在应该很好走。 –

+0

谢谢。你的答案解决了问题。但是,它运行加载locationUpdate方法(一种作弊方式)。我在onCreate的位置getter没有获取位置(返回我准备的Toast)。它不是一个解决方案吗?或者我删除这些行? –