【发布时间】:2016-01-31 06:50:40
【问题描述】:
我正在尝试使用实现DataApi.DataListener 的Activity 连接到可穿戴设备上的数据层。在onStart() 中,设备连接到Google API 客户端,并调用onConnected()。我从onConnected() 调用另一个方法,向手持设备发送消息以更新数据,并启动一个新的AsyncTask 以从数据层获取数据。这是我的Activity:
public class DataLayerActivity extends Activity implements DataApi.Listener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private ArrayList<myObject> mObjects = new ArrayList<>();
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
if(null != mGoogleApiClient && mGoogleApiClient.isConnected()) {
Wearable.DataApi.removeListener(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
} super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
Wearable.DataApi.addListener(mGoogleApiClient, this);
requestUpdate();
}
@Override
public void onConnectionSuspended(int cause) {
Timber.d("Connection Suspended");
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Timber.d("Connection Failed with result: " + result);
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
}
private void requestUpdate() {
new Thread(new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
for(Node node : nodes.getNodes()) {
Wearable.MessageApi.sendMessage(getGoogleApiClient(), node.getId(), "/path", null).setResultCallback(onMessageResult());
}
}
}).start();
try {
Uri uri = Uri.parse("/path");
mObjects = new FetchDataTask(this).execute(uri).get();
} catch(InterruptedException | ExecutionException exception) {
Timber.e(exception, "Fetch Data Failed");
}
}
protected ResultCallback<MessageApi.SendMessageResult> onMessageResult() {
if(!result.getStatus().isSuccess()) {
Timber.d("Failed to Connect with status " + result.getStatus());
}
}
}
这是FetchDataTask:
public class FetchDataTask extends AsyncTask<Uri, Void, ArrayList<myObject>> {
private Context mContext;
private ArrayList<myObject> mObjects = new ArrayList<>();
public FetchDataTask(Context context) {
mContext = context;
}
@Override
protected ArrayList<myObject> doInBackground(Uri... params) {
GoogleApiClient googleApiClient = new GoogleApiClient
.Builder(mContext).addApi(WearableAPI).build();
ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
if(!connectionResult.isSuccess() || !googleApiClient.isConnected()) {
Timber.e("Failed to Connect with error code: " + connectionResult.getErrorCode());
return null;
}
...
return mObjects;
}
}
连接结果总是返回error code 14,即连接超时。所以我尝试将已经连接的GoogleApiClient 从DataLayerActivity 作为参数传递给FetchDataTask,并将其用于进程。但是当我在doInBackground() 内拨打Wearable.DataApi.getDataItem().await() 或Werable.NodeApi.getLocalNode().await() 时,await() 永远不会结束,我的应用程序就会停止。但我从未收到来自onConnectionSuspended() 或onConnectionFailed() 的任何错误消息。我无法弄清楚为什么在我的AsyncTask 中与数据层的连接会超时。我知道这是可以做到的,因为它是在 XYZTouristAttractions 示例应用程序的 AttractionsActivity 中完成的。
【问题讨论】:
标签: android android-asynctask timeout wear-os android-wear-data-api