iT邦幫忙

DAY 6
0

x86 android 設備與外部硬體溝通研究系列 第 6

x86 android 設備與外部硬體溝通研究 - Android library project (6/30)

  • 分享至 

  • xImage
  •  

今天要簡單介紹使用自己撰寫的 android library project

  1. 首先,我們於eclipse中建android project 時,選擇建立為 Library Project。
  2. 在 ./src 中 add New Package , 使用你的library package name , ex : com.MyTestLibrary
  3. 在這個 android library project 中把 properties > android > library 設定為 Is Library
  4. 於 ./src/com.MyTestLibrary 中新增 class 例如下方範例

./src/com/MyTestLibrary/CountingSensorUpateRate.java

package com.MyTestLibrary;   // library package name !!!

import java.util.HashMap;
import java.util.Map;

public class CountingSensorUpateRate  {
// 寫一個簡單的 class , 計算 sensor 更新次數
	Map<String, Integer> map = new HashMap<String, Integer>();
	public int CountAs (String KeyWord){
		if(map.containsKey(KeyWord)){
			map.put(KeyWord, map.get(KeyWord)+1 );
		}else{
			map.put(KeyWord, 1 );
		}
		return map.get(KeyWord);
	}
	
	public int GetCounting(String KeyWord){
		return map.get(KeyWord);
	}
	
	public void Clear(){
		map.clear();
	}
}

在原本的android application project ,properties > android > library 加入上面新建的 library

修改原本的code 如下:

./src/com/example/test/MainActivity.java

package com.example.test;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.MyTestLibrary.CountingSensorUpateRate;  // 最重要的一行,引用剛剛新增的 library name "com.MyTestLibrary" , class name "CountingSensorUpdateRate"


public class MainActivity extends ActionBarActivity{

	private boolean timerHasStarted = false ;
	private Button startB;
	private TestCounting CountDownTimer;
	private TextView AccelX;
	private TextView AccelY;
	private TextView AccelZ;
	private TextView OrienX;
	private TextView OrienY;
	private TextView OrienZ;
	private SensorActivity MySensor;
	private TextView TimeView;
	private TextView ElapsedView;
	private TextView CountAccel,CountOrien;
	private long timeElapsed;
	private long startTime = 50*1000;
	private long interval = 1*1000; // 1 sec. per time interval
	private CountingSensorUpateRate MyCountingSensorUpdateRate; //宣告 CountingSensorUpdateRate
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startB = (Button) this.findViewById(R.id.button1);
        startB.setOnClickListener(new Button_Listener(this));
        AccelX = (TextView) this.findViewById(R.id.AccelX);
        AccelY = (TextView) this.findViewById(R.id.AccelY);
        AccelZ = (TextView) this.findViewById(R.id.AccelZ);
        OrienX = (TextView) this.findViewById(R.id.OrienX);
        OrienY = (TextView) this.findViewById(R.id.OrienY);
        OrienZ = (TextView) this.findViewById(R.id.OrienZ);
    	TimeView= (TextView) this.findViewById(R.id.timer);
		ElapsedView= (TextView) this.findViewById(R.id.timeElapsed);
        CountAccel = (TextView) this.findViewById(R.id.UpateRate_Accel);
        CountOrien = (TextView) this.findViewById(R.id.UpateRate_Orien);
        CountDownTimer = new TestCounting (startTime,interval);
        MyCountingSensorUpdateRate = new CountingSensorUpateRate(); // 新建一個 Counting object 

        MySensor = new SensorActivity();
        MySensor.onPause(); //預設啟動時停止



    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
   
    
    class Button_Listener implements OnClickListener {

    	  public Button_Listener(MainActivity activity) {

    	  }
    	  @Override
    	    public void onClick(View view) {
    	        if(!timerHasStarted){
    	        	CountDownTimer.start();
    	        	MySensor.onResume();

    	        	timerHasStarted = true;
    	        	startB.setText("RESET ME!");
    	        }else{
    	        	CountDownTimer.cancel();
    	        	MySensor.onPause();

    	        	timerHasStarted = false;
    	        	startB.setText("Start Counting... 50 Sec.");	
    	        }
    	    }
    	}
    public class TestCounting extends CountDownTimer{

		public TestCounting(long startTime, long interval) {
			super(startTime, interval);
		}
		@Override
		public void onFinish(){
			TimeView.setText("Time's Up");
			ElapsedView.setText("Time Elapsed:" + String.valueOf(startTime));
            // 在倒數計時結束時,顯示所有的計算結果,並清空計數器
			CountAccel.setText("Upate Accel "+ MyCountingSensorUpdateRate.GetCounting("TYPE_ACCELEROMETER") +" Times per "+startTime+" ms");
			CountOrien.setText("Upate Orien "+  MyCountingSensorUpdateRate.GetCounting("TYPE_ORIENTATION") +" Times per "+startTime+" ms");
			MyCountingSensorUpdateRate.Clear();
			MySensor.onPause();
		}
		@Override
		public void onTick(long millisUntillFinished){
			TimeView.setText("Time remain : "+ millisUntillFinished);
			timeElapsed = startTime - millisUntillFinished;
			ElapsedView.setText("Time Elapsed : " + String.valueOf(timeElapsed));
		}
    	
    
    }
    public class SensorActivity implements SensorEventListener {
        private SensorManager mSensorManager;

		public SensorActivity() {
            mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        }


		@SuppressWarnings("deprecation")
		protected void onResume() {
        	mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) , SensorManager.SENSOR_DELAY_NORMAL);
        	mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION) , SensorManager.SENSOR_DELAY_NORMAL);
        }

        protected void onPause() {
        	mSensorManager.unregisterListener(this);
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }


		@SuppressWarnings("deprecation")
		public void onSensorChanged(SensorEvent event) {
			switch(event.sensor.getType()){
			case Sensor.TYPE_ACCELEROMETER :
                // 藉由指定關鍵字,更新對應的計數器數值
				MyCountingSensorUpdateRate.CountAs("TYPE_ACCELEROMETER");
        		float X 	= event.values[0];
        		float Y 	= event.values[1];
        		float Z 	= event.values[2];
        		AccelX.setText("X : " + String.valueOf(X));
        		AccelY.setText("Y : " + String.valueOf(Y));
        		AccelZ.setText("Z : " + String.valueOf(Z));
				break;
			case Sensor.TYPE_ORIENTATION :
				MyCountingSensorUpdateRate.CountAs("TYPE_ORIENTATION");
        		float Z_Yaw 	= event.values[0];
        		float X_Pitch 	= event.values[1];
        		float Y_Roll 	= event.values[2];
        		OrienX.setText("X_Pitch : " + String.valueOf(X_Pitch));
        		OrienY.setText("Y_Roll : " + String.valueOf(Y_Roll));
        		OrienZ.setText("Z_Yaw : " + String.valueOf(Z_Yaw));
				break;
			}
        }
    }
    

}

./res/layout/activity_main.xml

<RelativeLayout 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="com.example.test.MainActivity" >

    <TextView
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/Time" /> 
        <!-- Call Resource String, 
        	需要到./res/values/strings.xml 
        	建立 Name => Value 對照
        	-->

     <TextView
         android:id="@+id/timeElapsed"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/timer"
         android:layout_below="@+id/timer"
         android:text="@string/Time_elasped" />

	<TextView
         android:id="@+id/Accel"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/button1"
         android:text="加速度計:" />
          <TextView
         android:id="@+id/AccelX"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/Accel"
         android:text="X" />
          <TextView
         android:id="@+id/AccelY"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/AccelX"
         android:layout_below="@+id/AccelX"
         android:text="Y" />
          <TextView
         android:id="@+id/AccelZ"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/AccelY"
         android:layout_below="@+id/AccelY"
         android:text="Z" />
         
          <TextView
         android:id="@+id/Orien"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/AccelZ"
         android:text="方位計:" />
         <TextView
         android:id="@+id/OrienX"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/Orien"
         android:text="X" />
          <TextView
         android:id="@+id/OrienY"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/AccelX"
         android:layout_below="@+id/OrienX"
         android:text="Y" />
          <TextView
         android:id="@+id/OrienZ"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/AccelY"
         android:layout_below="@+id/OrienY"
         android:text="Z" />
          
     <Button
         android:id="@+id/button1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/timeElapsed"
         android:layout_below="@+id/timeElapsed"
         android:text="Start Counting... 50 Sec." />
     <!-- Hardcode string 方式 -->

     <!-- 以下是今天新增的部分,拿來填寫計數器結果-->
     <TextView
         android:id="@+id/UpateRate_Accel"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/OrienZ"
         android:layout_below="@+id/OrienZ"
         android:text="" />
     <TextView
         android:id="@+id/UpateRate_Orien"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/OrienZ"
         android:layout_below="@+id/UpateRate_Accel"
         android:text="" />

</RelativeLayout>

我們明天見


上一篇
x86 android 設備與外部硬體溝通研究 - 更多的感應器 (5/30)
下一篇
x86 android 設備與外部硬體溝通研究 - JAR (7/30)
系列文
x86 android 設備與外部硬體溝通研究30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言