2012년 8월 6일 월요일

안드로이드 흔들기 모션 이벤트 캣치

스택 오버 플로우에 있는 걸 기본 프로그램에 넣어 본 것이다.

모션 이벤트 캣치 부분과 콜 로그 가져오는 부분을 합쳐 놓았다.

xml은 생략하는데 ID가 button1 이란 버튼 하나만 그려주면 된다.

android shake motion event
android how to get call log

------------------------------


import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private float mAccel;
private float mAccelCurrent;
private float mAccelLast;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
mSensorManager.registerListener(this, 
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
SensorManager.SENSOR_DELAY_NORMAL);    
mAccel = 0.00f;    
mAccelCurrent = SensorManager.GRAVITY_EARTH;   
mAccelLast = SensorManager.GRAVITY_EARTH; 
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(ContactsTask, "gettingcontacts").start();
}
});
}
@Override
public void onStop(){
super.onStop();
mSensorManager.unregisterListener(this); 
}

public void onResume() {
super.onResume();
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
SensorManager.SENSOR_DELAY_NORMAL);  
}
private Runnable ContactsTask = new Runnable() {
public void run() {
try {
getHistory();
}
catch(Exception e) {
e.printStackTrace();
}
}
};

private void getHistory() {
@SuppressWarnings("deprecation")
Cursor cur = managedQuery(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.TYPE + "= ?",
new String[] { String.valueOf(CallLog.Calls.OUTGOING_TYPE) }, CallLog.Calls.DEFAULT_SORT_ORDER);

Log.d("HERE", String.valueOf(cur.getCount()));
Log.d("HERE", CallLog.Calls.CONTENT_ITEM_TYPE);
Log.d("HERE", CallLog.Calls.CONTENT_TYPE);

if( cur.moveToFirst() && cur.getCount() > 0 ) {
while (cur.isAfterLast() == false) {
StringBuffer sb = new StringBuffer();
sb.append("call type=").append(cur.getString(cur.getColumnIndex(CallLog.Calls.TYPE)));
sb.append(", cashed name=").append(cur.getString(cur.getColumnIndex(CallLog.Calls.CACHED_NAME)));
sb.append(", content number=").append(cur.getString(cur.getColumnIndex(CallLog.Calls.NUMBER)));
sb.append(", duration=").append(cur.getString(cur.getColumnIndex(CallLog.Calls.DURATION)));
sb.append(", new=").append(cur.getString(cur.getColumnIndex(CallLog.Calls.NEW)));
       sb.append(", date=").append(timeToString(cur.getLong(cur.getColumnIndex(CallLog.Calls.DATE)))).append("]");
cur.moveToNext();

Log.e("HERE", sb.toString());
}
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

private String timeToString(Long time) {
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = simpleFormat.format(new Date(time));
return date;
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}

public void onSensorChanged(SensorEvent se) {
// TODO Auto-generated method stub
float x = se.values[0];     
    float y = se.values[1];   
    float z = se.values[2];       
    mAccelLast = mAccelCurrent;     
    mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));    
    float delta = mAccelCurrent - mAccelLast;     
    mAccel = mAccel * 0.9f + delta; 
    if(mAccel > 2) Toast.makeText(this, "SHAKE", Toast.LENGTH_SHORT).show();
}
}

댓글 2개:

  1. 저기 님 이거 웹뷰랑 넣으면 안되네요...
    어떻게 할 수 있나요?

    답글삭제

국정원의 댓글 공작을 지탄합니다.

UPBIT is a South Korean company, and people died of suicide cause of coin investment.

 UPBIT is a South Korean company, and people died of suicide cause of coin. The company helps the people who control the market price manipu...