android获取加速度传感器的程序问题
我现在想获取手机中的加速度传感器的变化值,并将它存入txt文档中,程序应该不难,但是我写出来发现文件穿件不出来,不知道哪里出问题了求高手指点代码如下:packagecom...
我现在想获取手机中的加速度传感器的变化值,并将它存入txt文档中,程序应该不难,但是我写出来发现文件穿件不出来,不知道哪里出问题了 求高手指点
代码如下 :
package com.example.getsensorvalue;
import java.io.*;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private SensorManager Sm;
private String a="";
private String b="";
private TextView TV1;
private Button Button1;
private Button Button2;
private File fos=null;
private Writer out=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV1=(TextView)findViewById(R.id.TV1);
Button1=(Button)findViewById(R.id.Button1);
Button1.setText(R.string.Button1);
Button2=(Button)findViewById(R.id.Button2);
Button2.setText(R.string.Button2);
Sm=(SensorManager)getSystemService(SENSOR_SERVICE);
Sensor Accelerate=Sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sm.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
b=b+event.values[0]+" "+event.values[1]+" "+event.values[2]+"\n";
a=event.values[0]+" "+event.values[1]+" "+event.values[2]+"\n";
TV1.setText(a);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, Accelerate,SensorManager.SENSOR_DELAY_NORMAL);
Button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
b="";
}
});
Button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
fos=new File(Environment.getExternalStorageDirectory().getPath() +"accelerate.txt");
fos.delete();
fos.createNewFile();
out=new FileWriter(fos);
out.write(b);
out.close();
}
catch(Exception e){
System.out.println(e);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(0,1,1,R.string.exit);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
finish();
}
return super.onContextItemSelected(item);
}
} 展开
代码如下 :
package com.example.getsensorvalue;
import java.io.*;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private SensorManager Sm;
private String a="";
private String b="";
private TextView TV1;
private Button Button1;
private Button Button2;
private File fos=null;
private Writer out=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV1=(TextView)findViewById(R.id.TV1);
Button1=(Button)findViewById(R.id.Button1);
Button1.setText(R.string.Button1);
Button2=(Button)findViewById(R.id.Button2);
Button2.setText(R.string.Button2);
Sm=(SensorManager)getSystemService(SENSOR_SERVICE);
Sensor Accelerate=Sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sm.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
b=b+event.values[0]+" "+event.values[1]+" "+event.values[2]+"\n";
a=event.values[0]+" "+event.values[1]+" "+event.values[2]+"\n";
TV1.setText(a);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, Accelerate,SensorManager.SENSOR_DELAY_NORMAL);
Button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
b="";
}
});
Button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
fos=new File(Environment.getExternalStorageDirectory().getPath() +"accelerate.txt");
fos.delete();
fos.createNewFile();
out=new FileWriter(fos);
out.write(b);
out.close();
}
catch(Exception e){
System.out.println(e);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(0,1,1,R.string.exit);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
finish();
}
return super.onContextItemSelected(item);
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询