Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3037406
  • 博文数量: 674
  • 博客积分: 17881
  • 博客等级: 上将
  • 技术积分: 4849
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 10:15
文章分类

全部博文(674)

文章存档

2013年(34)

2012年(146)

2011年(197)

2010年(297)

分类: LINUX

2011-05-18 16:30:19

一个从光传感器中读取数据的程序,并将其存储在sdcard文件中。如有写的不好的地方,请赐教。


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LightActivity extends Activity implements SensorEventListener{
private TextView tv_light;
private EditText et_light;
private Button buBegin;
private Button buEnd;
private Button buEnsure;//当要写入其他文件时,重新生成新的文件
private SensorManager sm;//SensorManager对象
private Sensor   lightSensor;
private float[]  lightValues = new float[1];//存储从传感器中读取的数据
private Timer updapteTime;
private TimerTask tt = null;
private  File     fi   = null;//判断文件是否存在
private  File     fidirectory = null;//判断文件夹是否存在
private  FileOutputStream fos = null;//向文件中写入数据
private Calendar   myCalendar;

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv_light = (TextView)findViewById(R.id.tv_light);
        buBegin = (Button)findViewById(R.id.buBegin);
        buEnd = (Button)findViewById(R.id.buEnd);
        buEnsure = (Button)findViewById(R.id.buEnsure);
        
        et_light = (EditText)findViewById(R.id.et_light);
        
        buBegin.setOnClickListener(new BeginClassListener());
        buBegin.setVisibility(View.INVISIBLE);
        buEnd.setOnClickListener(new EndClassListener());
        buEnd.setVisibility(View.INVISIBLE);
        buEnsure.setOnClickListener(new EnsureClassListener());
        
        
        sm = (SensorManager)getSystemService(SENSOR_SERVICE);
        lightSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
        sm.registerListener(this, lightSensor,SensorManager.SENSOR_DELAY_NORMAL);
        
        //程序开始时初始化文件夹,程序中的数据存与此文件夹
//该方法中默认文件存储在/sdcard/book/lightargu目录
        fidirectory = new File("/sdcard/");
        if(!fidirectory.exists()){
         fidirectory.exists();
        }
        fidirectory = new File("/sdcard/book/");
        if(!fidirectory.exists()){
         fidirectory.mkdir();
        }
        fidirectory = new File("/sdcard/book/lightargu");
        if(!fidirectory.exists()){
         fidirectory.mkdir();
        }

myCalendar = Calendar.getInstance();
    }
    public void updateGUI(){
     runOnUiThread(new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
tv_light.setText(lightValues[0]+"");
try {
fos.write((lightValues[0]+"\n").getBytes());
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Hello");
}
     });
    }

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(tt != null))
tt.cancel();
if(updapteTime != null)
updapteTime.cancel();
sm.unregisterListener(this);
}
//传感器SensorEventListener的监听函数
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
lightValues = event.values;
}
//Button控件的监听器
class BeginClassListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
updapteTime = new Timer("Light");
tt = new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
updateGUI();
}      
       };
updapteTime.scheduleAtFixedRate(tt, 0, 1000);
try {
fos = new FileOutputStream(fi.getAbsolutePath(),true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buBegin.setVisibility(View.INVISIBLE);
buEnd.setVisibility(View.VISIBLE);
}
}
class EndClassListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
fos.write((myCalendar.getTime().toString()+"\n").getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tt.cancel();
updapteTime.cancel();
//updapteTime.purge();
buBegin.setVisibility(View.VISIBLE);
buEnd.setVisibility(View.INVISIBLE);
}
}
class EnsureClassListener implements OnClickListener{
@Override
public void onClick(View v) {
String evlightstr = et_light.getText().toString();
if(!evlightstr.equals("")){
String filestr = "";
       filestr += "/sdcard/book/lightargu/"+et_light.getText().toString();    
       if(!filestr.contains(".")){
        filestr+= "hw.txt";
       fi = new File(filestr);
       if(!fi.exists()){
        System.out.println("creating it");
        try{
        fi.createNewFile();
        Toast.makeText(LightActivity.this, "File isn't exits.Now,create it.", Toast.LENGTH_LONG).show();
       
        }catch(IOException e){
        Toast.makeText(LightActivity.this, "新建文件失败!", Toast.LENGTH_LONG).show();
        e.printStackTrace();
        System.out.println(e.toString());
        finish();
        }
       }else{
        Toast.makeText(LightActivity.this, "文件存在!", Toast.LENGTH_LONG).show();
       }
       
       buBegin.setVisibility(View.VISIBLE);
       }else{
        et_light.setText("文件名不正确,有非法符号或者错误信息");
       }
}else{
Toast.makeText(LightActivity.this, "输入文件名空,重输入!", Toast.LENGTH_LONG).show();
}
}
}
阅读(1077) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~