我如何每隔3分钟通过程序触发按钮事件?

时间:2014-05-13 00:40:04

标签: android

我写了一个从服务器查询数据的视图,我希望用户可以使用按钮查询,如果他们没有任何动作,程序会每隔3分钟自动触发按钮事件?有人有个好主意吗?非常感谢。

public void jumpToLayout11(){

        StrictMode
        .setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
        .detectDiskReads()  
        .detectDiskWrites()  
        .detectNetwork()   // or .detectAll() for all detectable problems  
        .penaltyLog()  
        .build());  
     StrictMode
        .setVmPolicy(new StrictMode.VmPolicy.Builder()  
        .detectLeakedSqlLiteObjects()  
        .detectLeakedClosableObjects()  
        .penaltyLog()  
        .penaltyDeath()  
        .build());

     setContentView(R.layout.layout11);

        // 定義各 ListView 
        final ListView lvBKNO;
        final ListView lvFITX;
        final ListView lvFIMTX;
        final ListView lvOPTION;
        final ListView lvTOTAL;
        final TextView tvTime;




        // 各控件 findViewID

        lvBKNO = (ListView)findViewById(R.id.lvBKNO);
        lvFITX = (ListView)findViewById(R.id.lvFITX);
        lvFIMTX = (ListView)findViewById(R.id.lvFIMTX);
        lvOPTION = (ListView)findViewById(R.id.lvOPTION);
        lvTOTAL = (ListView)findViewById(R.id.lvTOTAL);         
        tvTime = (TextView)findViewById(R.id.tvTime);       


        Button btnLayout11Inq= (Button) findViewById(R.id.btnLayout11Inq);
        Button btnLayout11Back = (Button) findViewById(R.id.btnLayout11Back);



     btnLayout11Back.setOnClickListener(new OnClickListener()       
        {
            public void onClick(View v)
            {
             BackToMain();

            }                   
        });  


     btnLayout11Inq.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v){


                URL url;
                try {

                     url = new URL("http://172.0.0.1/mobiwork/mobiftsm.txt");

                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                    BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 



                    String data;
                    String bkno, fitx, fimtx, option, total;
                    String datatime;


                    ArrayList<String> arrBKNO = new ArrayList<String>();
                    ArrayList<String> arrFITX = new ArrayList<String>();
                    ArrayList<String> arrFIMTX = new ArrayList<String>();
                    ArrayList<String> arrOPTION = new ArrayList<String>();
                    ArrayList<String> arrTOTAL = new ArrayList<String>();

                    arrBKNO.add("據點");
                    arrFITX.add("大台指");
                    arrFIMTX.add("小台指");
                    arrOPTION.add("選擇權");
                    arrTOTAL.add("合    計");

                    try {
                          while ((data = in.readLine()) != null) {

                                 // 將讀取的 String Line substring 將各欄位填入 StringArray,再進 ListView Adapter:

                                    bkno = data.substring(0,4); 
                                    fitx = data.substring(5, 18);
                                    fimtx = data.substring(19,32);
                                    option = data.substring(33,46);
                                    total = data.substring(47,60);




                                    if (bkno.equals("1090") == true)                          
                                    {
                                        arrBKNO.add("總公司");
                                    }
                                    else
                                    if (bkno.equals("1091") == true)
                                    {
                                        arrBKNO.add("台中");
                                    }

                                    if (bkno.equals("1093") == true)                          
                                    {
                                        arrBKNO.add("埔里");
                                    }
                                    else
                                    if (bkno.equals("1095") == true)
                                    {
                                        arrBKNO.add("桃園");
                                    }

                                    if (bkno.equals("1096") == true)                          
                                    {
                                        arrBKNO.add("板橋");
                                    }
                                    else
                                    if (bkno.equals("1097") == true)
                                    {
                                        arrBKNO.add("高雄");                                      
                                    }
                                    else 
                                    if (bkno.equals("109A") == true)
                                    {
                                        arrBKNO.add("城中");
                                    }
                                    else
                                    if (bkno.equals("109K") == true) 
                                    {
                                        arrBKNO.add("忠孝");
                                    }
                                    else 
                                    if (bkno.equals("TOTA") == true)
                                    {  
                                        arrBKNO.add("總計");
                                    }



                                    arrFITX.add(fitx);
                                    arrFIMTX.add(fimtx);
                                    arrOPTION.add(option);
                                    arrTOTAL.add(total);



                            }    // while 迴圈結束

                            in.close();     
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }





                    // 設定分公司 lvBKNO ListView 的 ListAdapter      
                    final ArrayAdapter listAdapterBKNO;                    
                    listAdapterBKNO = new ArrayAdapter(MainActivity.this,R.layout.orangetextview,arrBKNO);
                    lvBKNO.setAdapter(listAdapterBKNO);                     
                    lvBKNO.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView parent, View view,int position,long id){
                            Toast.makeText(getApplicationContext()," ",Toast.LENGTH_SHORT ).show();                     
                            lvBKNO.setVisibility(view.VISIBLE);                         
                        }               
                    });      


                    // 設定大台指 lvFITX ListView 的 ListAdapter
                    final ArrayAdapter listAdapterFITX;                    
                    listAdapterFITX = new ArrayAdapter(MainActivity.this,R.layout.mytextview,arrFITX);
                    lvFITX.setAdapter(listAdapterFITX);                     
                    lvFITX.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView parent, View view,int position,long id){
                            Toast.makeText(getApplicationContext()," ",Toast.LENGTH_SHORT ).show();                     
                            lvFITX.setVisibility(view.VISIBLE);                         
                        }               
                    }); 

                    // 設定小台指 lvFIMTX ListView 的 ListAdapter
                    final ArrayAdapter listAdapterFIMTX;                       
                    listAdapterFIMTX = new ArrayAdapter(MainActivity.this,R.layout.mytextview,arrFIMTX);
                    lvFIMTX.setAdapter(listAdapterFIMTX);                       
                    lvFIMTX.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView parent, View view,int position,long id){
                            Toast.makeText(getApplicationContext()," ",Toast.LENGTH_SHORT ).show();                     
                            lvFIMTX.setVisibility(view.VISIBLE);                            
                        }               
                    }); 

                 // 設定選擇權 lvOPTION ListView 的 ListAdapter
                    final ArrayAdapter listAdapterOPTION;                      
                    listAdapterOPTION = new ArrayAdapter(MainActivity.this,R.layout.mytextview,arrOPTION);
                    lvOPTION.setAdapter(listAdapterOPTION);                     
                    lvOPTION.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView parent, View view,int position,long id){
                            Toast.makeText(getApplicationContext()," ",Toast.LENGTH_SHORT ).show();                     
                            lvOPTION.setVisibility(view.VISIBLE);                           
                        }               
                    });      



                    // 設定合計 lvTOTAL ListView 的 ListAdapter
                    final ArrayAdapter listAdapterTOTAL;                       
                    listAdapterTOTAL = new ArrayAdapter(MainActivity.this,R.layout.mytextview,arrTOTAL);
                    lvTOTAL.setAdapter(listAdapterTOTAL);                       
                    lvTOTAL.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView parent, View view,int position,long id){
                            Toast.makeText(getApplicationContext()," ",Toast.LENGTH_SHORT ).show();                     
                            lvTOTAL.setVisibility(view.VISIBLE);                            
                        }               
                    }); 





                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   

        }

        }); // 查詢結束

    }

2 个答案:

答案 0 :(得分:1)

让一个类称为MyTimeTask。

private class MyTimeTask extends TimerTask {
@Override
public void run() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
           //code you had in your onclick()

        }
    });
}
}

然后在你的活动中把它放在oncreate()

Timer myTimer = new Timer();
MyTimerTask myTimeTask= new MyTimeTask();

然后

//to Stop uncomment and put where you call your quit you don't have to
//myTimer.cancel();
//to start
myTimer.scheduleAtFixedRate(myTimerTask, 0, 180000); //(timertask,delay,period)

句点以毫秒为单位。

答案 1 :(得分:0)

我已经创建了一些制作处理程序线程的步骤

1.First convert minute to millisecond
  long t = (min * 60L) ;
  long result = TimeUnit.SECONDS.toMillis(t);
2.Create one thread we called acutaly Handler

Handler tick_Handler = new Handler();
MyThread tick_thread = new MyThread();

private class MyThread implements Runnable {
    public void run() {
            showTime("Data update or ui update");
            tick_Handler.postDelayed(tick_thread, result);
    }
} 

2.Start thread in onCreate method

/ **首次创建活动时调用。 * /

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      tick_Handler.post(tick_thread);
}