在服务中使用GoogleApiClient onConnected不会被调用 [英] GoogleApiClient onConnected not being called, using in a Service

查看:86
本文介绍了在服务中使用GoogleApiClient onConnected不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Activity中使用GoogleApiClient并将其移至服务且未调用onConnected时,它正在工作。

Was working when I was using GoogleApiClient in an Activity but moved it to a Service and the onConnected is not being called.

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在服务中工作吗?

Any ideas anyone? What am I doing wrong? Has anyone got GoogleApiClient working in a Service?

从活动调用服务

公共类MainActivity扩展了FragmentActivity {

public class MainActivity extends 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。从我所看到的日志中没什么特别的。

During debugging I can see the onCreate of the Service gets called, however the onConnected is never called. Nothing special in the logs from what I can see.

推荐答案

首先实现以下接口:


  1. GoogleApiClient.ConnectionCallbacks

  2. GoogleApiClient.OnConnectionFailedListener

然后,您将必须在类中添加以下方法:

Then you will have to add these methods to your class:


  1. public void onConnected(最终捆绑包)

  2. public void onConnectionSuspended(final int)

  3. public void onConnectionSuspended(final ConnectionResult connectionResult)

连接后,将立即调用 OnConnected 方法。通过这种方法,您可以做任何想要的事情,例如获取当前位置,位置地址,在地图上添加标记点等。

As soon as you connect, the OnConnected method will be called. In this method can you do whatever you want, like getting the current location, the location address, add a marker point to map, etc.

建立与 Google API客户端的连接。
要连接到 GoogleAPIClient ,您可以将此方法添加到您的类中,并在您的 onCreate 方法中调用它。

But you cannot do anything without establishing a connection to the Google API client. To connect to the GoogleAPIClient, you can add this method to your class and call it in your onCreate method.

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();
}

这篇关于在服务中使用GoogleApiClient onConnected不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆