我正在尝试获取加速度计值并在屏幕上显示。即使我没有收到任何错误,我也看不到任何值被发送到文本对象。 安装时我有振动许可,那些应该都很好。
package com.example.sendsms;
import android.app.Activity;
import android.content.Context;
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.widget.TextView;
import android.widget.Toast;
public class SendSMSActivity extends Activity implements SensorEventListener {
SendSMS mSender = new SendSMS();
private SensorManager sensorManager;
private Sensor accelerometer;
private float mSensorX;
private Sensor mSensor;
@Override
public void onSensorChanged(SensorEvent event) {
mSensorX = event.values[0];
String temp = Float.toString(mSensorX);
//mSensorY = event.values[1];
TextView resultText = (TextView)findViewById(R.id.xacc);
resultText.setText(temp);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get an instance of the SensorManager
this.sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
this.accelerometer = this.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
public void sendit(View v) {
boolean success = mSender.sendSMSMessage("0413556955",
// This is standard lorem-ipsum text, do not bother
// trying to wrap it, there's about 500 characters...
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
);
Toast.makeText(this, "Message sent " + (
success ? "successfully" : "unsuccessfully"),
Toast.LENGTH_SHORT).show();
TextView resultText = (TextView)findViewById(R.id.xacc);
resultText.setText("Message Sent");
}
}
答案 0 :(得分:0)
您需要注册听众。
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}