Notice
Recent Posts
Recent Comments
Link
관리 메뉴

설.현.아빠

[Shake] 흔들때마다 카운트 증가시키기, 조명유지, 세로화면유지 본문

안드로이드/Device & Sensor

[Shake] 흔들때마다 카운트 증가시키기, 조명유지, 세로화면유지

설.현.아빠 2011. 2. 11. 09:52




 

<?xml version="1.0" encoding="utf-8"?>

 

<!-- main.xml -->


<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:id="@+id/mainTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

 

 

<?xml version="1.0" encoding="utf-8"?>

 

<!-- AndroidManifest.xml -->

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="lee.android.Shake"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ShakeActivity"
                  android:label="@string/app_name"
                  android:screenOrientation="portrait">

                   <!--
                   세로화면 유지를 위한 screenOrientation설정.
                    -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
 <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

 <!--
 조명 유지를 위한 Permission설정.
 -->
</manifest>

 

 

 

// ShakeActivity.java

 

package lee.android.Shake;

import android.app.Activity;
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.os.PowerManager;
import android.widget.TextView;

public class ShakeActivity extends Activity implements SensorEventListener{
    
 private long lastTime;
 private float speed;
 private float lastX;
 private float lastY;
 private float lastZ;
 
 private float x, y, z;
 private static final int SHAKE_THRESHOLD = 1000; // 작을 수록 느린 스피드에서도 감지를 한다.
 
 private static final int DATA_X = SensorManager.DATA_X;
 private static final int DATA_Y = SensorManager.DATA_Y;
 private static final int DATA_Z = SensorManager.DATA_Z;
 
 private SensorManager sensorManager;
 private Sensor accelerormeterSensor;
 
    private TextView textview=null;
    private int count;
    
    private PowerManager pm; // 조명 유지
    private PowerManager.WakeLock wl; // 조명 유지

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerormeterSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        
        setContentView(R.layout.main);
        
        // 조명 유지
        pm= (PowerManager)getSystemService(Context.POWER_SERVICE);
        wl= pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");

    }
    
    @Override
    public void onStart() {
     super.onStart();
     
     if(accelerormeterSensor!=null) {
      sensorManager.registerListener(this, accelerormeterSensor, SensorManager.SENSOR_DELAY_GAME);
     }
     wl.acquire(); // 조명 유지
    }
    
    @Override
    public void onStop() {
     super.onStop();
     
     if(sensorManager != null) {
      sensorManager.unregisterListener(this);
     }
     wl.release(); // 조명 유지
    }
    
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
     // 정확도 설정
    }
    
    @Override
    public void onSensorChanged(SensorEvent event) {
     // Sensor 정보가 변하면 실행됨.
     if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
      long currentTime = System.currentTimeMillis();
      long gabOfTime = (currentTime - lastTime);
      //최근 측정한 시간과 현재 시간을 비교하여 0.1초 이상되었을 때 흔듬을 감지한다.
      if(gabOfTime > 100) {
       lastTime = currentTime;
       x = event.values[SensorManager.DATA_X];
       y = event.values[SensorManager.DATA_Y];
       z = event.values[SensorManager.DATA_Z];
       
       speed = Math.abs(x + y + z - lastX - lastY - lastZ)/gabOfTime * 10000;
       
       if(speed>SHAKE_THRESHOLD) {
        // 이벤트 발생!!
        textview = (TextView)findViewById(R.id.mainTextView);
        textview.setText(Integer.toString(++count));
       }
       lastX = event.values[DATA_X];
       lastY = event.values[DATA_Y];
       lastZ = event.values[DATA_Z];
      }
     }
    }
}


Comments