0

我一直試圖在我的Android應用程序中獲取設備的當前位置(而不是一直更新)整個一整天。我正在使用GoogleApiClient。由於位置總是返回null - 我讀到使用FusedLocationApi.requestLocationUpdates可能會修復它。所以我做到了。FusedLocationApi.requestLocationUpdates - onLocationChanged沒有被調用

現在它不工作了。這是我的代碼。如果有人告訴我該怎麼做,我會非常感激。

感謝先進!

package com.example.root.restaurant; 
    import android.Manifest; 
    import android.annotation.TargetApi; 
    import android.content.pm.PackageManager; 
    import android.location.Location; 
    import android.location.LocationManager; 
    import android.os.Build; 
    import android.provider.ContactsContract; 
    import android.support.annotation.NonNull; 
    import android.support.annotation.Nullable; 
    import android.support.annotation.RequiresApi; 
    import android.support.v4.app.ActivityCompat; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.widget.RelativeLayout; 
    import android.widget.Button; 
    import android.graphics.Color; 
    import android.widget.EditText; 
    import android.view.View; 
    import android.widget.TextView; 
    import android.content.Intent; 
    import android.widget.Toast; 

    import com.google.android.gms.common.ConnectionResult; 
    import com.google.android.gms.common.api.GoogleApiClient; 
    import com.google.android.gms.location.FusedLocationProviderApi; 
    import com.google.android.gms.location.LocationListener; 
    import com.google.android.gms.location.LocationRequest; 
    import com.google.android.gms.location.LocationServices; 
    import com.google.android.gms.maps.CameraUpdateFactory; 
    import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.model.LatLng; 
    import com.google.android.gms.maps.model.MarkerOptions; 

    import org.w3c.dom.Text; 

    import java.util.List; 

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

     private static GoogleApiClient googleApiClient; 
     private Location mLastLocation; 
     TextView t; 
     private Double myLatitude; 
     private Double myLongitude; 
     private LocationRequest locationRequest; 
     private FusedLocationProviderApi locationProvider = LocationServices.FusedLocationApi; 

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

    Log.i("jennysMsg", "..............................Started"); 

    t = (TextView) findViewById(R.id.textView2); 

    locationRequest = new LocationRequest(); 
    locationRequest.setInterval(15 * 1000); 
    locationRequest.setFastestInterval(15 * 1000); 
    locationRequest.setSmallestDisplacement(0); 
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 


    googleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .setAccountName("kfoozminus") 
      .build(); 
    if (googleApiClient != null) { 
     Log.i("jennysMsg", ".....................................it's not nul!!!"); 
     googleApiClient.connect(); 
    } 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
    if (location == null) { 
     requestLocationUpdates(); 
    } 
    else { 
     myLatitude = location.getLatitude(); 
     myLongitude = location.getLongitude(); 
     t.setText("Location : " + String.valueOf(myLatitude) + String.valueOf(myLongitude)); 
    } 
} 

private void requestLocationUpdates() { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

@Override 
public void onLocationChanged(Location location) { 

    myLatitude = location.getLatitude(); 
    myLongitude = location.getLongitude(); 
    t.setText("Location : " + String.valueOf(myLatitude) + String.valueOf(myLongitude)); 

} 

@Override 
protected void onStart() { 
    super.onStart(); 
    //googleApiClient.connect(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if(googleApiClient.isConnected()) { 
     requestLocationUpdates(); 
    } 
} 

@Override 
protected void onPause() { 
    super.onResume(); 
    LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    googleApiClient.disconnect(); 
} 
    } 

回答

0

所有你需要請檢查是否在您的manifest.Either ACCESS_COARSE_LOCATION,如果你想精確的位置大致位置或ACCESS_FINE_LOCATION增加許可的第一。

其次,你的代碼對我來說似乎很好,除非你需要從你的onStart方法取消註釋googleApiClient.connect()。

如果您使用模擬器無法返回位置,而是需要在模擬器旁邊創建自定義位置(經度,緯度),請點擊設置/更多圖標,您將能夠查找經度和緯度並設置經緯度手動點擊發送,你應該很好走。如果你仍然不工作,你可以嘗試重新啓動你的模擬器,或者使用真實的設備來測試這些代碼。

更新時間: 請檢查您的閹羊模擬器包括谷歌包,如果你不包括谷歌包,而你試圖訪問谷歌發揮servide它在你的谷歌播放服務將始終返回null

+0

一切你說,我做了所有。它不起作用。 googleApiClient.connect()一開始就在onStart()方法中,它不起作用,所以我將它放到了onCreate方法中。 我測試了genymotion(使用自定義的經度/緯度)和我的android手機。 它不工作:( – kfoozminus

相關問題