从logcat我意识到我的代码中只跳过了countDownTimer,而firebase监听器并不总是正常工作。 知道什么可能是错的吗?
我所要做的就是让我的客户等待雇主回复15秒,如果他在我们继续前进的时间内接受,如果不是我们尝试与列表中的下一个雇主等等。
我的代码:
package com.example.eltobgy.yala.Activities.OrderPackageActivities;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.example.eltobgy.yala.Activities.MainActivity;
import com.example.eltobgy.yala.Models.Package;
import com.example.eltobgy.yala.PackageOnTheWayActivity;
import com.example.eltobgy.yala.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;
import static com.example.eltobgy.yala.Activities.MainActivity.currentUser;
import static com.example.eltobgy.yala.Activities.MainActivity.mDatabaseUsersReference;
public class WaitingForDeliverymanAcceptanceActivity extends AppCompatActivity {
private static final String LOG_TAG=WaitingForDeliverymanAcceptanceActivity.class.getSimpleName();
private int i = 0;
private String id;
private DatabaseReference acceptedReference;
private String onlineSetPackage = "";
private Package mPackage;
boolean isAccepted;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watingfor_deliveryman_acceptance);
mPackage = (Package) getIntent().getSerializableExtra("package");
findDeliveryman();
}
private void findDeliveryman() {
Log.e("OrderPackageMapActivity", "entered find delivery man function");
while (i < OrderPackageMapActivity.onlineDeliveryMenList.size()) {
id = OrderPackageMapActivity.onlineDeliveryMenList.get(i).getId();
acceptedReference = MainActivity.mDatabaseOnlineDeliverymenReference.child(id).child("acceptedOrder");
acceptedReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if ((boolean) dataSnapshot.getValue() == true) {
isAccepted = true;
Log.e(LOG_TAG,"isAccepted is true");
} else {
isAccepted = false;
Log.e(LOG_TAG, "isAccepted is false");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
onlineSetPackage = OrderPackageMapActivity.onlineDeliveryMenList.get(i).getCurrentPackageID();
DatabaseReference onlineCurrentPackageRef = MainActivity.mDatabaseOnlineDeliverymenReference.child(id).child("currentPackageID");
if ((onlineSetPackage.equals("")) || (onlineSetPackage == null) || (onlineSetPackage.isEmpty())) {
onlineCurrentPackageRef.setValue(packageID);
MainActivity.currentUser.setAttachedDeliveryId(id);
Log.e(LOG_TAG,"package is set to:"+id);
CountDownTimer countDownTimer=new CountDownTimer(15000, 1000) {
public void onTick(long millisUntilFinished) {
//mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
Log.e(LOG_TAG,"seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
checkRequestAcceptance();
}
};
countDownTimer.start();
} else {
i++;
Log.e("i =", String.valueOf(i));
}
}
}
private void checkRequestAcceptance() {
if (isAccepted) {
Log.e(LOG_TAG, "I accepet");
mDatabaseUsersReference.child(currentUser.getId()).child("attachedDeliveryId").setValue(id);
Intent intent = new Intent(WaitingForDeliverymanAcceptanceActivity.this, PackageOnTheWayActivity.class);
startActivity(intent);
finish();
} else {
Log.e(LOG_TAG, "I refuse");
i++;
}
}
}