GoogleApiClient onConnected 未在 Watch 上调用
GoogleApiClient onConnected not called on Watch
我已经阅读了这个帖子,但我仍然遇到类似的问题:
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/phone 观看:创建中!
07-23 20:07:41.772 24874-24874/virtualgs.phonewatch D/phone 观看:开始!
已连接且未调用其他日志消息。我错过了什么吗?
尽管您的 activity 实现了 GoogleApiClient.ConnectionCallbacks 和 GoogleApiClient.OnConnectionFailedListener,但您从未将 activity 注册为接收这些回调的侦听器。在构建 api 客户端时,需要调用以下两个:addConnectionCallbacks(this) 和 addOnConnectionFailedListener(this):
client = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
我已经阅读了这个帖子,但我仍然遇到类似的问题:
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/phone 观看:创建中! 07-23 20:07:41.772 24874-24874/virtualgs.phonewatch D/phone 观看:开始!
已连接且未调用其他日志消息。我错过了什么吗?
尽管您的 activity 实现了 GoogleApiClient.ConnectionCallbacks 和 GoogleApiClient.OnConnectionFailedListener,但您从未将 activity 注册为接收这些回调的侦听器。在构建 api 客户端时,需要调用以下两个:addConnectionCallbacks(this) 和 addOnConnectionFailedListener(this):
client = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();