我使用handler.postDelayed()
方法重複任務與時間延遲..這是我的代碼;與handler.postDelayed()方法的問題
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void run() {
Toast.makeText(MapsActivity.this,pickup+" "+destination+" "+ Boolean.toString(requested),Toast.LENGTH_LONG).show();
}
}, 5000);
的問題是與變量(拾取,目的地,請求)例如請求其是boolean
我聲明瞭它在beggining爲假,之後我設置它真正在oncreate()
..並且在處理程序中,它每5秒鐘在真和假之間(這意味着在新值和舊值之間)改變值,與其他2個變量相同。
這是我的完整代碼:
String pickup="/",destination="/";
Boolean requested=false;
int seats=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if(getIntent().getExtras()!=null)
{
if(getIntent().getExtras().getString("class").equals("request")){
pickup = getIntent().getExtras().getString("pickup");
destination = getIntent().getExtras().getString("destination");
seats = getIntent().getExtras().getInt("seats");
if (!pickup.equals("")&&!pickup.equals("/")&&!destination.equals("")&&!destination.equals("/")&&seats!=0){
requested=true;
}
}
}
if (authenticate() == true) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void run() {
Toast.makeText(MapsActivity.this,pickup+" "+destination+" "+ Boolean.toString(requested),Toast.LENGTH_LONG).show();
handler.postDelayed(this, 5000);
}
}, 5000); }}
你可以發佈該類的完整代碼? – RdlP
好的,我會將它添加到主要問題中。 –