2012年12月15日 星期六

二秒推遲 距離感應器


package com.example.testlight;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.Vibrator;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.widget.TextView;

public class sv extends Service implements SensorEventListener,
Runnable {

private static final String TAG = "MainActivity";
private SensorManager mgr;
private Sensor proximity;
private Vibrator vibrator;
private float lastVal = -1;

@Override
public void onCreate() {
 super.onCreate();

 //Log.d(MainActivity.TAG, "onCreate...");
// Android 所有的感應器的統一介面
this.mgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
// 取得距離感應器
this.proximity = this.mgr.getDefaultSensor(Sensor.TYPE_PROXIMITY);
// 用振動來反應距離的變化
this.vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

//Log.d(MainActivity.TAG, "registerListener...");
// 一定要在這註冊
this.mgr.registerListener(this, this.proximity,
SensorManager.SENSOR_DELAY_NORMAL);
}
private Handler handler = new Handler();

@SuppressLint("Wakelock")
@Override
public void onSensorChanged(SensorEvent event) {
//Log.d(MainActivity.TAG, "onSensorChanged...");

// 目前的距離
float thisVal = event.values[0];
TextView textView1;
// textView1 = (TextView) findViewById(R.id.textView1);
// textView1.setText(Float.toString(thisVal));

if (thisVal == 0) {
// Write File"cover" to Internal Storage
String fileName = "cover";
String content = "0";
FileOutputStream writer = null;
try {
writer = openFileOutput(fileName, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.write(content.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// End of Write File"cover" to Internal Storage
// sleep
Thread thread = new Thread() {
@Override
public void run() {
try {
Thread.sleep(2500);
// Write File"cover" to Internal Storage
String fileName1 = "cover";
String content1 = "1";
FileOutputStream writer1 = null;
try {
writer1 = openFileOutput(fileName1,
Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer1.write(content1.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// End of Write File"cover" to Internal Storage
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
};
// 開始執行執行緒
thread.start();

} else {
// Read in. st. from File"cover"
String fileName2 = "cover";
int readed;
String content2 = "";
byte[] buff = new byte[256]; // input stream buffer
// Input stream
try {
FileInputStream reader = openFileInput(fileName2);
// FileInputStream reader =
// context.getApplicationContext().openFileInput(fileName);
while ((readed = reader.read(buff)) != -1) {
content2 += new String(buff).trim();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// End of Read in. st. from File"cover"

// Toast.makeText(getApplicationContext(),
// "001",Toast.LENGTH_SHORT).show();
String ct2rr = content2;
if (ct2rr.equalsIgnoreCase("1")) {
this.vibrator.vibrate(100);
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
                WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
                wakeLock.acquire();
}

}

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void run() {
// TODO Auto-generated method stub

}

}

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...