안드로이드

 

 

 

 

 

class MainActivity

package kr.co.braverokmc.mysensor;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    SensorManager manager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.textView);


        manager =(SensorManager)getSystemService(Context.SENSOR_SERVICE);

    }



    public void onClick(View v){
       List<Sensor>  sensorList  =manager.getSensorList(Sensor.TYPE_ALL);

        int num =0;
       for(int i=0; i<sensorList.size(); i++){

           Sensor curSensor =sensorList.get(i);
           String curName=curSensor.getName();

           if(curName.contains("Orientation")){
               num=i;
           }

           println("Sensor : #" +i + " : " +curName);

       }

        MySensonListener listener =new MySensonListener();
        manager.registerListener(listener, sensorList.get(num), SensorManager.SENSOR_DELAY_UI);


    }


    class MySensonListener implements SensorEventListener{
        @Override
        public void onSensorChanged(SensorEvent event) {
            long timestamp =event.timestamp;
            println("value : " + timestamp +" : " + event.values[0]);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    }


    private void println(String data){
        textView.append(data+"\n");
    }




}










 

 

R.layout.activity_main

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="kr.co.braverokmc.mysensor.MainActivity">

    <Button
        android:text="센서확인하기"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button"
        android:onClick="onClick"
        />


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/button"
        android:background="@android:color/holo_orange_light">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:id="@+id/textView"
            />



    </ScrollView>





</RelativeLayout>

 

 

 

 

 

android

 

about author

PHRASE

Level 60  머나먼나라

진실로 이르노니 너희가 믿음이 겨자씨 만큼만 있으면, 능히 못할 것이 없느니라.

댓글 ( 4)

댓글 남기기

작성