我使用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
我在开始时将其声明为假,之后我将其设置为真在oncreate()
..并且在处理程序中它的值每5秒在true和false之间变化(这意味着新值和旧值之间的平均值)..与另一个相同的事情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); }}
答案 0 :(得分:0)
handler.postDelayed()没问题,如果你想要切换值,试试吧。 pickup =!pickup,destination =!destination,...