免责声明:我知道还有其他类似的问题,但我想尝试为其他人创建一个超级简单的案例来重现问题
在过去的几个月里,我一直在使用与下面的代码非常相似的东西,使用google play服务请求位置更新,它运行良好。现在由于某种原因onlocationchanged不再被调用。发生在整个测试设备范围内。我现在使用Play Services 7.0.0。我请求更新,但什么也没发生。我怀疑它与我最近进行的一次代码更新有关(尽管这与定位系统无关),但我无法找到修复方法。当我恢复提交时,我仍然有一个问题——关于运行良好的旧代码!
我已经将这段代码设置为一个全新的示例应用程序,并使用locationrequest对象的所有选项来获得乐趣,但什么也没有…在调用onconnected之后,日志“onconnected,requestLocationUpdates”之后,一切都将停止。
谢谢你的帮助!
相似的:
Android GPS onLocationChanged never called
android onLocationChanged never called
onLocationChanged() never called
相关清单条目:

<application
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
     .....
</application>

<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

样本活动:
public class MyActivity extends Activity {

Context act = this;
private GoogleApiClient mGoogleApiClient;
String TAG = "LocationService.java";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    new MyLocationCallbacks().buildGoogleApiClient();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private class MyLocationCallbacks implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

    public void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(act)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        //Log.i(TAG, "Connected yet? " + mGoogleApiClient.isConnecting() + " " + mGoogleApiClient.isConnected());
        mGoogleApiClient.connect();
        //Log.i(TAG, "Connected yet? " + mGoogleApiClient.isConnecting() + " " + mGoogleApiClient.isConnected());
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.i(TAG, "Connected to GoogleApiClient");
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setExpirationDuration(30000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        Log.d(TAG, "onConnected, requestLocationUpdates");
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
    }

    @Override
    public void onLocationChanged(Location locationA) {
        Log.d(TAG, locationA.getLongitude() + " :: " + locationA.getLatitude());
    }


    @Override
    public void onConnectionSuspended(int i) {
        Log.d(TAG, "onConnectionSuspended called");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d(TAG, "onConnectionFailed called");
    }

}

}

最佳答案

我喜欢在找到答案之前先发问…
上面的代码运行良好,只是没有设置间隔。在locationrequest上,只要调用setinterval(无论需要什么)。晚安。
https://github.com/googlesamples/android-play-location

09-30 15:13