Android Studio'仅限设备' Google Play服务的位置无效

时间:2015-09-04 13:32:53

标签: android google-play-services

我正在开发我的第一个应用程序,它是一个非常简单的应用程序。单击按钮将经度/纬度发送到服务器/数据库。我正在使用Google Play服务进行定位,而我正在使用Nexus 9平板电脑进行测试。 我发现使用设置>位置(在Nexus 9上) - 高精度&低精度工作正常。数据正确并发送到服务器。但是当选择“仅设备”(GPS)时,它找不到长/纬。我得到了祝酒词:

uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" 

我还是新手,所以我不确定我哪里出错了。我确实有清单文件w /

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

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

我也放了

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

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

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

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关闭的日志备注。

ArrayAdapter

Wifi关闭了最后一行。

1 个答案:

答案 0 :(得分:1)

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

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

有关详细信息,您可以尝试我的代码here