Skip to content

Instantly share code, notes, and snippets.

@rinevo
Last active February 19, 2017 12:19
Show Gist options
  • Save rinevo/bc09587628c8e9d0a397a0a938d39b47 to your computer and use it in GitHub Desktop.
Save rinevo/bc09587628c8e9d0a397a0a938d39b47 to your computer and use it in GitHub Desktop.
近接センサータイマー
<?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="com.rinevo.texttest.MainActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="" />
</ScrollView>
</RelativeLayout>
package com.rinevo.texttest;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ScrollView;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private TextView mTextView;
private ScrollView mScrollView;
private SensorManager mSensorManager;
private long mBackTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 表示を縦固定にする
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// スリープしないようする
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// 操作するViewをリソースから取得
mTextView = (TextView)findViewById(R.id.textView);
mScrollView = (ScrollView)findViewById(R.id.scrollView);
// センサーを取得
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
// イベントリスナーにセンサーを登録
Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onPause() {
super.onPause();
// イベントリスナーからセンサーを解除
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
// 近接センサーのとき
if (sensorEvent.sensor.getType() == Sensor.TYPE_PROXIMITY) {
// 現在の日時をミリ秒単位で取得
long currentTimeMillis = System.currentTimeMillis();
Date date = new Date(currentTimeMillis);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
// 前回との時間差を求める
if (mBackTime == 0) {
mBackTime = currentTimeMillis;
}
double diffTime = ((double) currentTimeMillis - (double) mBackTime) / 1000.0;
mBackTime = currentTimeMillis;
// 日時と差分の文字列
String sTime = simpleDateFormat.format(date) + " +" + String.valueOf(diffTime);
// 出力
if (sensorEvent.values[0] == sensorEvent.sensor.getMaximumRange()) {
mTextView.append("D: " + sTime + "\n");
} else {
mTextView.append("N: " + sTime + "\n");
}
// スクロールを末尾に移動
mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment