Android之迴圈佇列操作
阿新 • • 發佈:2018-12-31
佇列特性:先進先出(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> <ScrollView android: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() { @Override public 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() { @Override public 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); } } }); @Override protected 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); } @Override protected 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() { @Override public 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,所以需要+1 mDay = 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.執行效果
開始執行
暫停執行
清除
重新開始