Android プログラミング デバイス制御

センサ

public class SensorEx extends Activity implements SensorEventListener{

    private final static String BR = System.getProperty("line.separator");
    private final static int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
    private TextView textView;
    private SensorManager manager;

//    回転行列
    private float[] inR = new float[16];
    private float[] outR = new float[16];

    private float[] accValues = new float[3]; //加速度
    private float[] magValues = new float[3]; //地磁気
    private float[] oriValues = new float[3]; //端末の傾き
    private boolean accEnabled = false; // 加速度センサーの有無
    private boolean magEnabled = false; // 地磁気センサーの有無

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        LinearLayout layout = new LinearLayout(this);
        layout.setBackgroundColor(Color.WHITE);
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);

        textView = new TextView(this);
        textView.setText("SensorEx");
        textView.setTextSize(24);
        textView.setTextColor(Color.BLACK);
        textView.setLayoutParams(new LinearLayout.LayoutParams(WC, WC));
        layout.addView(textView);

//        センサマネージャの取得
        manager = (SensorManager)getSystemService(
                Context.SENSOR_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();

//        センサの取得
        List<Sensor> sensors = manager.getSensorList(Sensor.TYPE_ALL);
        for(Sensor sensor:sensors){
            int type = sensor.getType();
//            加速度センサのリスナー登録
            if(type == Sensor.TYPE_ACCELEROMETER){
                manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
                accEnabled = true;
            }
            // 地磁気センサのリスナー登録
            if(type == Sensor.TYPE_MAGNETIC_FIELD){
                manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
                magEnabled = true;
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if(accEnabled || magEnabled){
            manager.unregisterListener(this);
            accEnabled = false;
            magEnabled = false;
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if(event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) return;

//        センサのタイプ取得
        int type = event.sensor.getType();

//        加速度センサの情報の格納
        if(type == Sensor.TYPE_ACCELEROMETER){
            accValues = event.values.clone();
        }
        // 地磁気センサの情報の格納
        else if(type == Sensor.TYPE_MAGNETIC_FIELD){
            magValues = event.values.clone();
        }

        if(accEnabled && magEnabled){

//            回転行列の計算
            SensorManager.getRotationMatrix(inR,null,accValues,magValues);
//            端末の向きに応じた軸の変更
            SensorManager.remapCoordinateSystem(inR,
                    SensorManager.AXIS_X, SensorManager.AXIS_Y, outR);
//          端末の傾きの計算
            SensorManager.getOrientation(outR, oriValues);
//          ラジアンを度に変換
            oriValues[0] = (float)Math.toDegrees(oriValues[0]);
            oriValues[1] = (float)Math.toDegrees(oriValues[1]);
            oriValues[2] = (float)Math.toDegrees(oriValues[2]);
        }

//        文字列表示
        StringBuffer sb = new StringBuffer();
        sb.append("SensorEx>" + BR);
        if(accEnabled){
            sb.append("加速度[x軸]:"+fm(accValues[0])+BR);
            sb.append("加速度[y軸]:"+fm(accValues[1])+BR);
            sb.append("加速度[z軸]:"+fm(accValues[2])+BR+BR);
        }
        if(accEnabled && magEnabled) {
            sb.append("ピッチ[x軸]:" + fm(oriValues[0]) + BR);
            sb.append("ロール[y軸]:"+fm(oriValues[1])+BR);
            sb.append("アジマス[z軸]:"+fm(oriValues[2])+BR+BR);
        }
        textView.setText(sb.toString());
    }

//    センサの制度の変更時に呼ばれる
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

//    数値のフォーマット
    private String fm(float value){
        return (value <= 0) ? ""+value:"+"+value;
    }
}

位置情報

まず、Manifest.xmlに書き込む。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

次にコード。

ublic class LocationEx extends Activity implements LocationListener {

    private final static String BR = System.getProperty("line.separator");
    private final static int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
    private TextView textView;
    private LocationManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        LinearLayout layout = new LinearLayout(this);
        layout.setBackgroundColor(Color.WHITE);
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);

        textView = new TextView(this);
        textView.setText("LocationEx");
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(24);
        textView.setLayoutParams(new LinearLayout.LayoutParams(WC, WC));
        layout.addView(textView);

//        ロケーションマネージャの取得
        manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }

//    アクティビティ再開時に呼ばれる
    @Override
    protected void onResume() {
        super.onResume();
//        ロケーションマネージャのリスナー登録
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    }

//    アクティビティ一時停止に呼ばれる
    @Override
    protected void onPause() {
        super.onPause();
//        ロケーションマネージャの設定
        manager.removeUpdates(this);
    }

//    位置情報変更を通知する時に呼ばれる
    @Override
    public void onLocationChanged(Location location) {
        textView.setText("LocationEx>" + BR +
        "緯度"+location.getLatitude()+BR+
        "軽度"+location.getLongitude());
    }

//  位置情報取得有効化を通知する時に呼ばれる
    @Override
    public void onProviderEnabled(String provider) {
    }

//  位置情報取得無効化を通知する時に呼ばれる
    @Override
    public void onProviderDisabled(String provider) {

    }

//    位置情報状態変更を通知する時に呼ばれる
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}

参考文献