Android之迴圈隊列操作

來源:互聯網
上載者:User

標籤:android   迴圈隊列   

隊列特性:先進先出(FIFO)——先進隊列的元素先出隊列。

來源於我們生活中的隊列(先排隊的先辦完事)。

下面以一個簡單的例子實現迴圈隊列的操作。


1.建立Android應用程式



2.介面上添加按鈕

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity"    android:orientation="vertical" >        <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button             android:id="@+id/start"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="開始"/>        <Button             android:id="@+id/pause"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="暫停"/>        <Button             android:id="@+id/clear"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="清除"/>    </LinearLayout>    <ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent">    <TextView         android:id="@+id/MonitorData"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:scrollbars="vertical"        android:singleLine="false"        android:textSize="18sp"        android:textStyle="normal"        android:textColor="#000"android:text="@string/hello_world"/></ScrollView></LinearLayout>


3.處理效果

package com.sl.queuedemo;import java.util.Calendar;import java.util.Timer;import java.util.TimerTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.SystemClock;import android.annotation.SuppressLint;import android.app.Activity;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;//線程發送資料入隊,定時器定時處理資料public class MainActivity extends Activity{private static final String TAG = "QueueDemo";static final int REFRESH = 0;private TextView mTextView;public Button m_btnStart = null;public Button m_btnPause = null;public Button m_btnClear = null;private boolean m_bIsReading = false;//控制線程運行private boolean m_bIsRunning = false;//隊列操作    public int m_nWt = 0;public int m_nRd = 0;public int m_nCnt=0;private final byte MAX_QUEUE_SIZE=20;private final byte MAX_ELEMENT_SIZE=6;public byte m_ucMonitorData[][] = new byte[MAX_QUEUE_SIZE][MAX_ELEMENT_SIZE];private int nLength = 6;private byte TxBuffer[] = new byte[nLength];private byte RxBuffer[] = new byte[nLength];//時間操作private int mYear;private int mMonth;private int mDay;private int mHour;private int mMinute;private int mSecond;public String m_sMonitorTime[] = new String[MAX_QUEUE_SIZE];//同步設定private Object Mutex = new Object();//定時器設定Timer timer = new Timer();TimerTask task = new TimerTask(){@Overridepublic void run(){Message message = new Message();message.what = REFRESH;handler.sendMessage(message);}};//線程處理@SuppressLint("HandlerLeak")Handler handler = new Handler(){public void handleMessage(Message msg){switch (msg.what){case REFRESH:RefreshData();break;default:break;}}};//線程public Thread myThread = new Thread(new Runnable(){@Overridepublic void run(){while(m_bIsRunning){TxBuffer[0]++;TxBuffer[1]++;TxBuffer[1]++;TxBuffer[2]++;TxBuffer[3]++;TxBuffer[4]++;TxBuffer[4]++;TxBuffer[5]++;TxBuffer[5]++;TxBuffer[5]++;InQueue(TxBuffer, nLength);SystemClock.sleep(1000);}}});@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextView = (TextView)findViewById(R.id.MonitorData);m_btnStart = (Button)findViewById(R.id.start);m_btnPause = (Button)findViewById(R.id.pause);m_btnClear = (Button)findViewById(R.id.clear);m_btnStart.setOnClickListener(listener);m_btnPause.setOnClickListener(listener);m_btnClear.setOnClickListener(listener);        m_bIsRunning = true;        myThread.start();timer.schedule(task,1000,1000);}@Overrideprotected void onDestroy() {super.onDestroy();m_bIsRunning = false;timer.cancel();timer.purge();}public void RefreshData(){if(m_bIsReading){String str = GetOutQueueString();mTextView.setText(str);}}OnClickListener listener = new View.OnClickListener() {@Overridepublic void onClick(View v) {switch(v.getId()){case R.id.start:m_bIsReading = true;RefreshData();break;case R.id.pause:m_bIsReading = false;break;case R.id.clear:ResetQueue();RefreshData();break;default:break;}}};public void InQueue(final byte TxBuffer[],final int nLength){synchronized (Mutex) {if(nLength<=0){return;}//入隊時間final Calendar c = Calendar.getInstance();mYear = c.get(Calendar.YEAR);mMonth = c.get(Calendar.MONTH) + 1;//擷取的月份比實際月份小1,所以需要+1mDay = c.get(Calendar.DAY_OF_MONTH);mHour = c.get(Calendar.HOUR_OF_DAY);mMinute = c.get(Calendar.MINUTE);mSecond = c.get(Calendar.SECOND);String year = "0" + mYear;year = year.substring(year.length()-2, year.length());String month = "0" + mMonth;month = month.substring(month.length()-2, month.length());String day = "0" + mDay;day = day.substring(day.length()-2, day.length());String hour = "0" + mHour;hour = hour.substring(hour.length()-2, hour.length());String minute = "0" + mMinute;minute = minute.substring(minute.length()-2, minute.length());String second = "0" + mSecond;second = second.substring(second.length()-2, second.length());String str = year + "." + month + "." + day + " " + hour + ":" + minute + ":" + second + " -- ";m_sMonitorTime[m_nWt] = str;//入隊資料for(int i =0;i<nLength;i++){m_ucMonitorData[m_nWt][i] = TxBuffer[i];}m_nWt++;if(m_nWt >= MAX_QUEUE_SIZE){m_nWt = 0;}m_nCnt++;if(m_nCnt > MAX_QUEUE_SIZE){m_nCnt = MAX_QUEUE_SIZE;m_nRd++;if(m_nRd >= MAX_QUEUE_SIZE){m_nRd = 0;}}}}public boolean OutQueue(byte RxBuffer[]){synchronized (Mutex) {if(m_nCnt <= 0){return false;}for(int i=0;i<MAX_ELEMENT_SIZE;i++){RxBuffer[i] = m_ucMonitorData[m_nRd][i];}m_nRd++;if(m_nRd >= MAX_QUEUE_SIZE){m_nRd = 0;}m_nCnt--;return true;}}public String GetOutQueueString(){synchronized (Mutex) {String strline = "";String str = "";int index = m_nRd;for(int i=0;i<m_nCnt;i++){strline = strline + m_sMonitorTime[index];//時間for(int j=0;j<MAX_ELEMENT_SIZE;j++){strline = strline + str.format("%02X ",m_ucMonitorData[index][j]);//資料}strline = strline + "\n";//換行index++;if(index >= MAX_QUEUE_SIZE){index = 0;}}return strline;}}public void ResetQueue(){synchronized (Mutex) {m_nCnt = 0;m_nWt = 0;m_nRd = 0;Log.d(TAG, "重設隊列");}}}


4.運行效果

開始運行

 


暫停運行


清除



重新開始

 


源碼下載


Android之迴圈隊列操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.