我使用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
好的,我会将它添加到主要问题中。 –