iT邦幫忙

DAY 4
0

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

x86 android 設備與外部硬體溝通研究 - 加速度 (4/30)

  • 分享至 

  • xImage
  •  

直接看這兩段從昨天的 Timer 改進而來的 Code :

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener; //#
import android.widget.TextView; //#
import android.widget.Button; // #
import android.os.CountDownTimer; // #
import android.view.View; //#
import android.hardware.SensorEvent; //以下都是本次使用Sensor 必要class
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager;
import android.hardware.Sensor;



public class MainActivity extends ActionBarActivity{

	//private MalibuCountDownTimer countDownTimer;
	private boolean timerHasStarted = false ;
	private Button startB;
	private ThisIsATimer CountDownTimer;
	private TextView TimeView;
	private TextView ElapsedView;
	private TextView AccelX;
	private TextView AccelY;
	private TextView AccelZ;
	private long timeElapsed;
	private final long startTime = 50*1000;
	private final long interval = 1*1000; // 1 sec. per time interval
	private SensorActivity MySensor;
	
    @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));
        TimeView = (TextView) this.findViewById(R.id.timer);
        AccelX = (TextView) this.findViewById(R.id.AccelX);
        AccelY = (TextView) this.findViewById(R.id.AccelY);
        AccelZ = (TextView) this.findViewById(R.id.AccelZ);
        ElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
        CountDownTimer = new ThisIsATimer(startTime, interval);

        MySensor = new SensorActivity(); // oncreate 時,宣告註冊sensor manager 
        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);
    }
    
    
    public class ThisIsATimer extends CountDownTimer{

		public ThisIsATimer(long startTime, long interval) {
			super(startTime, interval);
		}
		@Override
		public void onFinish(){
			TimeView.setText("Time's Up");
			ElapsedView.setText("Time Elapsed:" + String.valueOf(startTime));
		}
		@Override
		public void onTick(long millisUntillFinished){
			TimeView.setText("Time remain : "+ millisUntillFinished);
			timeElapsed = startTime - millisUntillFinished;
			ElapsedView.setText("Time Elapsed : " + String.valueOf(timeElapsed)); 
		}
    	
    }
    
    class Button_Listener implements OnClickListener {
    	  //private MainActivity activity;
    	  public Button_Listener(MainActivity activity) {
    	   // this.activity = activity;
    	  }
    	  @Override
    	    public void onClick(View view) {
    	        if(!timerHasStarted){
    	        	CountDownTimer.start();
    	        	MySensor.onResume(); // 由這邊啟動,sensor 監聽
    	        	timerHasStarted = true;
    	        	startB.setText("RESET ME!");
    	        }else{
    	        	CountDownTimer.cancel();
    	        	MySensor.onPause(); // 暫停
    	        	timerHasStarted = false;
    	        	startB.setText("Start Counting... 50 Sec.");
    	        	
    	        }
    	    }
    	}
    public class SensorActivity implements SensorEventListener {
        private final SensorManager mSensorManager;
        private final Sensor mAccelerometer;

        public SensorActivity() {
            mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); //向系統要求使用 sensor 
            mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); // 註冊使用 加速度計 
        }

        protected void onResume() {//恢復時,監聽sensor 變化

            mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        }

        protected void onPause() {//暫停,監聽sensor 變化

            mSensorManager.unregisterListener(this);
        }

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


		public void onSensorChanged(SensorEvent event) {//把 sensor 資訊顯示到 textview 中
                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));
        	
        	
        }
    }
}






<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/AccelX"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/button1"
         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" />
     <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 方式 -->
          

</RelativeLayout>

我們明天見


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

尚未有邦友留言

立即登入留言