当我在android中调用onDatachanged时,我试图了解事件的流程。据我所知,程序的理想流程应首先打印“Inside onclick”然后“在Ondatachanged内部”和“onclick的结束”然而流程我得到的是随机的,对于第二和第三行。任何人都可以解释我如何使流量线性化,为什么它是非线性的?另外,当我第一次输入数据时,不会调用Ondatachanged。为什么是什么?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAmbientEnabled();
//Google client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
Log.d("Inside", "onConnected " );
}
@Override
public void onConnectionSuspended(int cause) {
Log.d("Inside", "onConnectionSuspended: " + cause);
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d("Inside", "onConnectionFailed: " + result);
}
})
// Request access only to the Wearable API
.addApiIfAvailable(Wearable.API)
.build();
mGoogleApiClient.connect();
Intent service= new Intent(this,WearbleService.class);
startService(service);
Wearable.DataApi.addListener(mGoogleApiClient, this);
//Listener for button
Button done=(Button)findViewById(R.id.button);
done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Inside","Onclick");
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
int count=0;
putDataMapReq.getDataMap().putString("key", "HelloWOrld");
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
// putDataMapReq.getDataMap().putString("key", "helloWorld");
putDataReq = putDataMapReq.asPutDataRequest();
pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
Log.d("End of ", "Onclick " );
}
});
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
Log.d("Inside","OndataChanged");
}