Android Studio 'Device Only' Location w/ Google Play 服务不工作

Android Studio 'Device Only' Location w/ Google Play Services not working

我正在开发我的第一个应用程序,它是一个非常简单的应用程序。单击按钮将经度/纬度发送到 server/database。我正在使用 Google Play 服务定位,我正在使用 Nexus 9 平板电脑进行测试。 我发现使用 Settings>Location (on Nexus 9) - High Accuracy & Low Accuracy 效果很好。数据正确并发送到服务器。 But when choosing 'Device Only'(GPS) it does not find the Long/Lat.我敬酒:

Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();

我还是个新手,所以我不确定我可能哪里出错了。我确实有带有

的清单文件
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" 

和 Google 在我的 Gradle 文件中播放服务。因为无论如何它都可以使用 WiFi。

我也放了

System.out.println("Longitude (function name)" + longitude + " and Latitude: " + latitude);

当 Long/Lat 被拾取时在日志中查看(只有当按钮被点击时,否则它是 0.0)在我上次测试中我使用了高精度并得到了 Long/Lat。关闭 Wifi 并更改位置,它正在获取我的新位置(但无法发送数据,因为没有互联网连接)。所以我很困惑,因为 GPS 不是在高精度中获取我的新位置吗?

不确定这是否重要,但我希望“仅设备”适用于定位和高精度。 任何帮助将不胜感激!这是我的代码(已缩短,如果我遗漏了任何内容,请告诉我添加!)谢谢!

public class MainActivity extends Activity implements ConnectionCallbacks,
    OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private Button btnNewShowLocation;
private double longitude;
private double latitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (checkPlayServices()) {
        buildGoogleApiClient();
        //**UPDATED**
        createLocationRequest()
    }
    btnNewShowLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mLastLocation != null) {
                postDataToServer("web site address- EDIT");
            } else {
                Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();
            }}});}
    // **UPDATED** 
    protected void createLocationRequest() {
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            .setInterval(UPDATE_INTERVAL)
            .setFastestInterval(FATEST_INTERVAL)
            .setSmallestDisplacement(DISPLACEMENT);

private void postDataToServer(String uri) {

    RequestPackage p = new RequestPackage();
    p.setMethod("POST");
    p.setUri(uri);
    p.setParam("longitude", String.valueOf(longitude));
    p.setParam("latitude", String.valueOf(latitude));
    MyTask task = new MyTask();
    task.execute(p);
    System.out.println("Longitude (post data to server)" + longitude + " and Latitude: " + latitude);

}
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();
}
private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "This device is not supported.", Toast.LENGTH_LONG)
                    .show();
            finish();
        }
        return false;
    }
    return true;
}
@Override
public void onConnected(Bundle arg0) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    System.out.println("Longitude (on Connected): " + longitude + " and Latitude: " + latitude);

    if (mLastLocation != null) {
        latitude = mLastLocation.getLatitude();
        longitude = mLastLocation.getLongitude();
    } else {
        Toast.makeText(getApplicationContext(), "Searching for your location... \nMake sure WiFi or GPS is turned On", Toast.LENGTH_LONG).show();
    }}}

这里是高精度开启和 Wifi 关闭的日志记录。

I/System.out﹕ Longitude (on Connected): 0.0 and Latitude: 0.0
I/System.out﹕ Longitude (post data to server)-88.7777777 and Latitude: 55.7777777
W/System.err﹕ java.net.UnknownHostException: Unable to resolve host

最后一行的 Wifi 已关闭。

您需要创建 LocationRequest 对象并执行以下操作:

// Create the LocationRequest object
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
                .setInterval(interval)
                .setFastestInterval(fastestInterval)
                .setSmallestDisplacement(minDisplacement);

更多详情,你可以试试我的代码here