2017-06-05 64 views
2

关于同样的问题,还有一些关于SO的其他问题,但我猜测答案有点旧,可能不适用于我的场景。在下面的代码中,方法onLocationChanged从不被调用。我也尝试从android模拟器发送新的位置,但它从来没有被调用过。任何想法在这里出了什么问题?onLocationChanged从未在FusedLocationApi中调用过

public class MyActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, LocationListener { 

    private GoogleApiClient apiClient; 

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

     apiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 

     apiClient.connect(); 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     System.out.println("yeah, this works"); 

     // I also tried to set some properties on the location request like interval, etc. but they have no effect 
     LocationRequest request = new LocationRequest(); 
     LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, request, this); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     System.out.println("this is never shown"); 
    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     System.out.println("this is never shown"); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     System.out.println("this line is never called "); 
    } 
} 
+0

你有权限吗? – gaurang

+0

通过这个例子一次,你可以从这里解决它:http://javapapers.com/android/android-location-fused-provider/ –

+0

嘿@shaft我认为你必须首先检查谷歌播放是否可用和第二次检查位置提供商之后,进一步在代码 – santoXme

回答

0

我发现我有关于“所有com.android.support库必须使用完全相同的版本规范”的gradle警告。以我为例,我改变

compile 'com.android.support:appcompat-v7:25.2.0' 
compile 'com.android.support:support-v4:25.2.0' 
compile 'com.android.support:design:25.2.0' 
compile 'com.android.support:recyclerview-v7:25.2.0' 
compile 'com.google.android.gms:play-services:9.8.0' 
compile 'com.android.support.constraint:constraint-layout:1.0.2' 

compile 'com.android.support:appcompat-v7:25.2.0' 
compile 'com.android.support:animated-vector-drawable:25.2.0' 
compile 'com.android.support:support-core-ui:25.2.0' 
compile 'com.android.support:mediarouter-v7:25.2.0' 
compile 'com.android.support:support-v4:25.2.0' 
compile 'com.android.support:design:25.2.0' 
compile 'com.android.support:recyclerview-v7:25.2.0' 
compile 'com.android.support.constraint:constraint-layout:1.0.2' 
compile 'com.google.android.gms:play-services:9.8.0' 

有了新的依赖列表,gradle这个是开心的例子只是工作。希望这可以帮助某人。

相关问题