GoogleApiClient onConnected未被调用,正在服务中使用



当我在一个活动中使用GoogleApiClient时,我正在工作,但将其移动到了一个服务,并且没有调用onConnected。

public class StepsMonitoringService extends Service implements GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient mClient;
@Override
public IBinder onBind(Intent arg0) {
    return null;
}
@Override 
public void onCreate() {
    super.onCreate();
    mClient = new GoogleApiClient.Builder(this).addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
            .addConnectionCallbacks(this).build();
    mClient.connect();  
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;    
}
@Override
public void onDestroy() {
    super.onDestroy();
    mClient.disconnect();
}
@Override
public void onConnected(Bundle connectionHint) {
   // NOT being called!! WHY??
}
@Override
public void onConnectionSuspended(int cause) {
}

}

有什么想法吗?我做错了什么?有人让GoogleApiClient在服务中工作吗?

该服务是从活动调用的

公共类MainActivity扩展FragmentActivity{

private TextView mStepsView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(this, StepsMonitoringService.class);
    startService(intent);
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("StepMonitoringServiceIntent"));
    mStepsView = (TextView) findViewById(R.id.steps);
}
private void displayOnUI(String msg) {
    mStepsView.setText(msg + "n" + mStepsView.getText());
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        long steps = intent.getLongExtra("steps", 0);
        displayOnUI(steps + " steps");
    }
};

}

在调试过程中,我可以看到调用了服务的onCreate,但从未调用过onConnected。据我所见,日志中没有什么特别之处。

首先实现这些接口:

  1. GoogleApiClient.ConnectionCallbacks
  2. GoogleApiClient.OnConnectionFailedListener

然后你必须将这些方法添加到你的类中:

  1. public void onConnected(最终捆绑包)
  2. public void onConnectionSuspended(最终int i)
  3. public void onConnectionFailed(最终ConnectionResult ConnectionResult)

连接后,将立即调用OnConnected方法。在这种方法中,你可以做任何你想做的事情,比如获取当前位置、位置地址、添加标记点到地图等。

但是,如果不建立到Google API客户端的连接,则无法执行任何操作。要连接到GoogleAPIClient,您可以将此方法添加到类中,并在onCreate方法中调用它。

private synchronized void buildGoogleAPIClient(){
    googleApiclient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    googleApiclient.connect();
    Toast.makeText(getActivity(), "connected", Toast.LENGTH_SHORT).show();
}

addOnConnectionFailedListener添加到GoogleApiClient以跟踪错误。根据在将客户端连接到服务时出现错误时调用的文档addOnConnectionFailedListener

public class StepsMonitoringService extends Service implements GoogleApiClient.ConnectionCallbacks {
    private GoogleApiClient mClient;
    @Override 
    public void onCreate() {
        super.onCreate();
        mClient = new GoogleApiClient.Builder(this).addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
            .addConnectionCallbacks(this)
            //Add Connection Failed Listener to track error.
            .addOnConnectionFailedListener(this)
            .build();
        mClient.connect();  
    }
    @Override
    public void onConnected(Bundle connectionHint) {
       // NOT being called!! WHY??
    }
    @Override
    public void onConnectionSuspended(int cause) {
    }
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
         //Called called when there was an error connecting the client to the service. 
        Log.i(LOG_TAG,"onConnectionFailed:"+connectionResult.getErrorCode()+","+connectionResult.getErrorMessage());
    }
}

试试这个。

public class StepsMonitoringService extends Service implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {
    /**
     * Provides the entry point to Google Play services.
     */
    protected GoogleApiClient mClient;
    /**
     * Builds a GoogleApiClient. Uses the addApi() method to request the
     * Fitness API.
     */
    protected synchronized void buildGoogleApiClient() {
        mClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Fitness.HISTORY_API)
                .build();
    }
    @Override
    public void onCreate() {
        super.onCreate();
        if (mClient == null) {
            buildGoogleApiClient();
        }
        mClient.connect();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }
    @Override
    public void onConnected(Bundle connectionHint) {
        // NOT being called!! WHY??
    }
    @Override
    public void onConnectionSuspended(int cause) {
        mClient.connect();
    }
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // Called called when there was an error connecting the client to the
        // service.
        Log.i(LOG_TAG,
                "onConnectionFailed:" + connectionResult.getErrorCode() + "," + connectionResult.getErrorMessage());
    }
}

我在使用useDefaultAccount()创建客户端时遇到了同样的问题。然后我用setAccountName()替换了它,它就开始工作了。我认为由于某些原因,accountpicker在服务中不起作用,这就是为什么googleapiclient的connect()静默失败的原因,因为您需要指定用于检索健身信息的帐户。注意,您需要在活动中有一个帐户选择器,并以某种方式将电子邮件帐户传递给您的服务。

相关内容

  • 没有找到相关文章

最新更新