2014年5月25日 星期日

[Android] Android G-sensor

Android G-sensor

device027  
小試身手前的小知識
  1. 宣告一個SensorManager,SensorManager管理、調度、處理sensor的工
  2. getSystemService(SENSOR_SERVICE)取得感測器服務
  3. 設定取得的感測器類型 getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
  4. 需要與感測器互動時,必須先注冊藉此監視與感測器相關的活動,方法為SensorManger類別中的registerListener
  5.  利用SensorEventListener監聽Sensor之兩件事, (a) onSensorChanged(SensorEvent event)當感測器的值有所改變。 (b)onAccuracyChanged(Sensor sensor, int accuracy)感測器精準度改變時。
  6.  Sensor更新速度如下表,控制方式為程式碼淺藍色標記部分
  Sensor.manager.SENSOR_DELAY_FASTEST  0ms
  Sensor.manager.SENSOR_DELAY_GAME  20ms
  Sensor.manager.SENSOR_DELAY_UI  60ms
  Sensor.manager.SENSOR_DELAY_NORMAL  200ms

小試身手小範例
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
java檔
package www.gsensor;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity implements SensorEventListener {

        private TextView text_x;
        private TextView text_y;
        private TextView text_z;
        private SensorManager aSensorManager;
        private Sensor aSensor;
        private float gravity[] = new float[3];
        /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.fragment_main);
        text_x = (TextView)findViewById(R.id.TextView01);
        text_y = (TextView)findViewById(R.id.TextView02);
        text_z = (TextView)findViewById(R.id.TextView03);

        aSensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
        aSensor = aSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        aSensorManager.registerListener(this, aSensor, SensorManager.SENSOR_DELAY_NORMAL);
        /*
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        */
    }

    @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub
               
        }
    @Override
    public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    gravity[0] = event.values[0];
    gravity[1] = event.values[1];
    gravity[2] = event.values[2];
    text_x.setText("X = "+gravity[0]);
    text_y.setText("Y = "+gravity[1]);
    text_z.setText("Z = "+gravity[2]);
    }
    @Override
    protected void onPause()
    {
    // TODO Auto-generated method stub
    /* 取消註冊SensorEventListener */
    aSensorManager.unregisterListener(this);
    Toast.makeText(this, "Unregister accelerometerListener", Toast.LENGTH_LONG).show();
    super.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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }



}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
xml檔
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="三軸加速度 - 資料擷取"
android:textSize="25sp"
android:layout_margin="15sp"
android:layout_marginBottom="10sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X"
android:id="@+id/TextView01"
android:textSize="25sp"
android:layout_margin="15sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y"
android:id="@+id/TextView02"
android:textSize="25sp"
android:layout_margin="15sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z"
android:id="@+id/TextView03"
android:textSize="25sp"
android:layout_margin="15sp"
/>
</LinearLayout>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------







資料來源

沒有留言:

張貼留言