2016-09-29 89 views
0

我使用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); }} 
+0

你可以发布该类的完整代码? – RdlP

+0

好的,我会将它添加到主要问题中。 –

回答

0

不是问题handler.postDelayed(),试试吧,如果你想切换值。 皮卡=!皮卡,目标=!目的地,...

+0

我已经在切换值:“requested = true;”在oncreate()中,并且它在5秒内显示为true,然后显示false。 –

+0

我真的不理解你的期望。请再次清除它。你的期望是你想显示真实的,虚假的,真实的,虚假的,......或者它没有你的手柄自动真正的假? –

+0

我的期望是不显示:true,false,true,false,...这就是它自身的问题......我的期望是设置例如按钮中的变量的值,并用处理程序发送这些更改 –