所以我找到了关于GoogleApiClient的一些不太清楚的东西. GoogleApiClient有一个名为onConnected的函数,它在客户端连接时运行(肯定)
我得到了自己的函数:startLocationListening,最终在GoogleApiClient的onConnected函数上调用.
所以我的startLocationListening函数无法在没有GoogleApiClient连接的情况下运行.
代码和日志:
@Override
public void onConnected(Bundle bundle) {
log("Google_Api_Client:connected.");
initLocationRequest();
startLocationListening(); //Exception caught inside this function
}
Run Code Online (Sandbox Code Playgroud)
...
private void startLocationListening() {
log("Starting_location_listening:now");
//Exception caught here below:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
Run Code Online (Sandbox Code Playgroud)
例外是:
03-30 12:23:28.947: E/AndroidRuntime(4936): java.lang.IllegalStateException: GoogleApiClient is not connected yet.
03-30 12:23:28.947: E/AndroidRuntime(4936): at com.google.android.gms.internal.jx.a(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936): at com.google.android.gms.common.api.c.b(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936): at com.google.android.gms.internal.nf.requestLocationUpdates(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936): at hu.company.testproject.service.GpsService.startLocationListening(GpsService.java:169)
03-30 12:23:28.947: E/AndroidRuntime(4936): …Run Code Online (Sandbox Code Playgroud) android google-api-client google-play-services android-googleapiclient
我们在崩溃时遇到了这种崩溃,奇怪的是它在onConnected()请求位置更新时会在回调中发生.
码:
abstract public class MainService_6_LocationClient extends MainService_5_DriverGpsLocationStoring
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private LocationListener highAccuracyListener;
private GoogleApiClient googleApiClient;
private LocationRequest gpsRequest;
@Override
public void onCreate() {
super.onCreate();
lazyInit();
googleApiClient.connect();
}
@Override public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected");
lazyInit();
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, gpsRequest,
highAccuracyListener);
}
private void lazyInit() {
if (highAccuracyListener == null) {
highAccuracyListener = new HighAccuracyLocationListener();
}
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
if (gpsRequest == null) {
gpsRequest = new …Run Code Online (Sandbox Code Playgroud) 我无法将高分数提交给Android排行榜.现在排行榜是空的,没有提交任何内容.我有一个需要提交的int"oldScore".现在我正在尝试一段代码来提交它,但活动在被调用时崩溃了.我的代码:
public class GameOver extends BaseGameActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
public static int score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_over);
mGoogleApiClient.connect();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
int newScore = GameOver.score;
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int oldScore = prefs.getInt("key", 0);
if (newScore > oldScore) {
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("key", newScore);
edit.commit();
EditText HighScore = (EditText) findViewById(R.id.HighScoreT);
HighScore.setText("" + newScore);
} else {
EditText HighScore = (EditText) findViewById(R.id.HighScoreT);
HighScore.setText("" + oldScore); …Run Code Online (Sandbox Code Playgroud)