2012年12月15日 星期六

light sensor 亮度


package com.example.testlight;

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

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
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.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

protected SensorManager sensorMgr;
protected List<Sensor> lightSensor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLightSensor();
registListener();
}

private boolean getLightSensor() {
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
lightSensor = sensorMgr.getSensorList(Sensor.TYPE_LIGHT);

if (lightSensor.isEmpty()) {
String msg = "Init fail:No LightSensor";
Log.d("Test05", msg);
return false;
}
return true;
}

private boolean registListener() {
boolean bResult;
bResult = sensorMgr.registerListener(mListener, lightSensor.get(0),
SensorManager.SENSOR_DELAY_FASTEST);

if (!bResult) {
String msg = "Init fail:Can't regist listener";
Log.d("Test05", msg);
return false;
}
return true;
}

private void unregistListener() {
if (sensorMgr != null) {
sensorMgr.unregisterListener(mListener);
}
}

protected final SensorEventListener mListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}

@Override
public void onSensorChanged(SensorEvent event) {
String msg = "現在的亮度是 " + event.values[0] + " Lux";
// TextView tv = (TextView) findViewById(R.id.textView1);
// tv.setText(msg);
if (event.values[0] < 4.2) {
noti("You covered your phone."+ event.values[0]);
logzero();
}else{
if(firsted()!=1){
logfirst();
noti("You uncovered your phone.");
}

}

}

private void logzero() {
// TODO Auto-generated method stub
String fileName = "firsted";
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();
}
}

private int firsted() {
int out = 0;
// TODO Auto-generated method stub
String fileName = "firsted";
int readed; // 已讀取的位元數
String content = ""; // 內容
byte[] buff = new byte[256]; // input stream buffer
// Input stream
try {
FileInputStream reader = openFileInput(fileName);
while ((readed = reader.read(buff)) != -1) {
content += new String(buff).trim();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (content.toCharArray()[0] == '1')
out=1;

return out;
}

private void logfirst() {
// TODO Auto-generated method stub
String fileName = "firsted";
String content = "1";
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();
}
}

private void noti(String msg) {
// TODO Auto-generated method stub
NotificationManager gNotMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 產生Notification物件,並設定基本屬性
Notification tBNot = new Notification(R.drawable.ic_launcher, msg,
System.currentTimeMillis());

Intent notificationIntent = new Intent(getBaseContext(),
MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
getBaseContext(), 0, notificationIntent, 0);
tBNot.setLatestEventInfo(getBaseContext(), "Light Sensor", msg,
contentIntent);
gNotMgr.notify(1, tBNot);

}
};

}

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...