我已阅读此帖,但我仍面临类似的问题:
GoogleApiClient onConnected never called on Wearable device
我试着详细了解其工作原理: https://developer.android.com/training/wearables/data-layer/events.html
以下是我的代码:
public class Main extends Activity implements DataApi.DataListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String PATH = "/phonewatch";
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("phone watch", "On Create!");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
client = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
...
}
@Override
protected void onStart() {
Log.d("phone watch", "On Start!");
super.onStart();
client.connect();
}
@Override
protected void onStop() {
Log.d("phone watch", "On Stop!");
if (client != null && client.isConnected()) {
Wearable.DataApi.removeListener(client, this);
client.disconnect();
}
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
Log.d("phone watch", "On connected! Add listener.");
Wearable.DataApi.addListener(client, this);
}
@Override
public void onConnectionSuspended(int i) {
Log.d("phone watch", "connection suspended.");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("phone watch", "connection failed.");
}
@Override
public void onDataChanged(final DataEventBuffer dataEventBuffer) {
Log.d("phone watch", "Data changed!");
...
}
我只有这个:
07-23 20:07:41.730 24874-24874 / virtualgs.phonewatch D /手表观看:On Create! 07-23 20:07:41.772 24874-24874 / virtualgs.phonewatch D /手表观看:开始!
未连接已连接和其他日志消息。我错过了什么吗?
答案 0 :(得分:2)
虽然您的活动实现了GoogleApiClient.ConnectionCallbacks和GoogleApiClient.OnConnectionFailedListener,但您从未将您的活动注册为侦听器以接收这些回调。构建api客户端时,需要调用以下两个:addConnectionCallbacks(this)和addOnConnectionFailedListener(this):
client = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();