OnLocationChanged在许多设备上不再工作

时间:2015-03-21 02:07:11

标签: android google-play-services locationlistener

免责声明:我知道还有其他类似的问题,但我想尝试为其他人创建一个更简单的案例来重新创建问题

过去几个月,我一直在使用与以下代码非常相似的内容来使用Google Play服务请求位置更新,并且工作正常。现在由于某种原因onLocationChanged不再被调用。发生在各种测试设备上。我现在正在使用Play Services 7.0.0。我要求更新,没有任何反应。我怀疑它与我最近的代码更新有关(尽管与位置系统无关),但我无法找到修复程序。当我还原提交时,我仍然有问题 - 在旧代码上工作正常!

我已将此代码设置为一个全新的示例应用程序,并使用locationRequest对象的所有选项进行娱乐,但没有任何内容......一切都在日志之后停止," onConnected,requestLocationUpdates"调用onConnected后。

感谢您的帮助!

类似:

  1. Android GPS onLocationChanged never called
  2. android onLocationChanged never called
  3. onLocationChanged() never called
  4. 相关舱单条目:

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

    }

1 个答案:

答案 0 :(得分:3)

我喜欢在找到答案之前发布问题的方式......

上面的代码工作正常,我只是没有设置间隔。在LocationRequest上,只需调用setInterval(无论需要什么)......晚安。

https://github.com/googlesamples/android-play-location

相关问题