모션 이벤트 캣치 부분과 콜 로그 가져오는 부분을 합쳐 놓았다.
xml은 생략하는데 ID가 button1 이란 버튼 하나만 그려주면 된다.
android shake motion event
android how to get call log
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();
}
}
저기 님 이거 웹뷰랑 넣으면 안되네요...
답글삭제어떻게 할 수 있나요?
그건 저도 잘... :)
삭제