2016-03-02 109 views
0

我想要做這個簡單的任務。我有兩個按鈕叫做START和STOP,我想通過點擊START開始執行一些任務,然後通過點擊STOP來退出這個循環。 代碼-與其他點擊Interuppting onClick()方法

public class DrawPath extends Activity implements View.OnClickListener { 
ArrayList<LatLng> positions = new ArrayList<LatLng>() ; 
static int c=1; 

Location location; 
GoogleMap mMap; 
Button btStart,btStop; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.drawpath); 
    initializeVar(); 
    btStart.setOnClickListener(this); 
    btStop.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      c = 0; 
      System.out.println("tested2"); 
     } 
    }); 
} 
private void initializeVar() 
{ 
    btStart=(Button)findViewById(R.id.btStart); 
    btStop=(Button)findViewById(R.id.btStop); 
} 

@Override 
public void onClick(View v) { 
    getupdate(1); 
    } 

private void getupdate(int d) { 
    c = d; 
    CurrentPosition currentPosition = new CurrentPosition(this); 
    if (c == 0) { 
     System.out.println("Done"); 
    } else { 
     location = currentPosition.getCurrentLocation(); 
     LatLng pos = new LatLng(location.getLatitude(), location.getLongitude()); 
     positions.add(pos); 
     System.out.println("running"); 
     try { 
      Thread.sleep(5000); 
      getupdate(c); 
     } catch (Exception e) { 
     } 
    } 
} 
} 

有人請分享任何想法如何實現它。

回答

0

您可以使用Handler和Runnable在STOP按鈕單擊後停止線程。 我給你按你的要求

Handler handler = new Handler(); 
Runnable runable = new Runnable({ 
    @Override 
    public void run(){ 
     // count 
     handler.postDelayed(this, 1000); 
    } 
}); 

現在你可以調用從btnStop.onClick以下行()提示使用下面的代碼。

handler.removeCallbacks(runable); 

Check this關於處理器更多細節和Runnable接口

+0

我沒弄清楚@Rakesh – Ashu

0

我建議它所創建延伸Thread,並根據用戶的行動開始一個內部類和停止線程。這裏有一個例子

class DrawPath extends Activity implements View.OnClickListener { 

    MyThread thread; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.drawpath); 
     initializeVar(); //not in my code so you add it 
     btStart.setOnClickListener(this); 
     btStop.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.btStart: 
       if (thread == null) { 
        thread = new MyThread(); 
        thread.start(); 
       } 
       break; 

      case R.id.btStop: 
       if (thread != null) { 
        thread.interrupt(); 
       } 
       break; 
     } 
    } 

    class MyThread extends Thread { 
     @Override 
     public void run() { 
      while (true) { 
       try { 
        Thread.sleep(3000); 
        //your stuff goes here or before sleep 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
        thread = null; 
        break; 
       } 
      } 
     } 
    } 

    //whey interrupt here bcz infinite loop will be running until and unless you stop it. 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     if (thread != null) 
      thread.interrupt(); 
    } 
} 

我看見你的代碼需要多一點的改進,這就是爲什麼我寫了這個大文件:)

建議:

  1. 結帳onClickListener實施。
  2. 在onDestroy()上停止線程,因爲即使在關閉應用程序後線程仍將運行,因此當您的主要活動出現(銷燬)時,需要停止線程。

Happy_Coding;