关于 android:OnLocationChanged 将不再工作,在许多设备上

OnLocationChanged Won't Work Anymore, on many devices

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

在过去的几个月里,我一直在使用与下面的代码非常相似的东西来使用谷歌播放服务请求位置更新,并且运行良好。现在由于某种原因 onLocationChanged 不再被调用。发生在整个测试设备范围内。我现在使用的是 Play Services 7.0.0。我要求更新,但没有任何反应。我怀疑它与我最近所做的代码更新有关(尽管与定位系统无关),但我无法找到修复程序。当我恢复提交时,我仍然遇到问题 - 在运行良好的旧代码上!

我已将此代码设置到一个全新的示例应用程序中,并使用 locationRequest 对象的所有选项来取乐,但什么也没有……在调用 onConnected 后,日志后一切都停止,"onConnected, requestLocationUpdates" .

感谢您的帮助!

类似:

  • Android GPS onLocationChanged 从未调用过
  • android onLocationChanged 从未调用过
  • onLocationChanged() 从未调用过
  • 相关清单条目:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <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" />

    示例活动:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    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