码迷,mamicode.com
首页 > 移动开发 > 详细

Android之循环队列操作

时间:2015-02-03 09:36:49      阅读:190      评论:0      收藏:0      [点我收藏+]

标签: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>

    <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.运行效果

开始运行

技术分享 技术分享


暂停运行

技术分享


清除

技术分享

重新开始

技术分享 技术分享


源码下载


Android之循环队列操作

标签:android   循环队列   

原文地址:http://blog.csdn.net/bingdianlanxin/article/details/43417607

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!