OnLocationChanged 将不再工作,在许多设备上

Posted

技术标签:

【中文标题】OnLocationChanged 将不再工作,在许多设备上【英文标题】:OnLocationChanged Won't Work Anymore, on many devices 【发布时间】:2015-05-24 13:36:55 【问题描述】:

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

在过去的几个月里,我一直在使用与下面的代码非常相似的东西来使用谷歌播放服务请求位置更新,并且运行良好。现在由于某种原因 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");
    


【问题讨论】:

请在发布问题之前进行适当的研究。谢谢! 【参考方案1】:

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

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

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

【讨论】:

过去,当设备拾取其初始位置时,onLocationChanged 被调用。我想知道是否有什么改变。我也有这个问题。

以上是关于OnLocationChanged 将不再工作,在许多设备上的主要内容,如果未能解决你的问题,请参考以下文章

应用程序在后台时没有 GPS onLocationChanged 回调?

onLocationChanged 从未调用过?

未从 IntentService 调用 onLocationChanged

onLocationChanged 在每个时间间隔调用两次

为啥 Android 服务中没有调用 OnlocationChanged?

onLocationChanged 在应用程序启动时被多次调用