今天写kotlin的计步器和加速度传感器,百度地图,还有用java的仿摇一摇功能
加速度传感器
class AeecActivity:Activity(),SensorEventListener {
var systemService:SensorManager? = null;
var textView:TextView ? = null;
var sensor:Sensor? = null;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_aeec)
initview()
initSensor();
}
private fun initSensor() {
systemService = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensor = systemService!!.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
}
private fun initview() {
textView = findViewById(R.id.acce_text)
}
//传感值变化回调
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
}
//传感器事件
override fun onSensorChanged(event: SensorEvent?) {
val values = event?.values
var x = values!![0]
var y = values!![1]
var z = values!![2]
textView?.text = "X轴加速度"+x+"\nY轴加速度"+y+"\nZ轴加速度"+z
}
override fun onResume() {
super.onResume()
// 注册传感器事件
systemService?.registerListener(this,sensor,200)
}
override fun onPause() {
super.onPause()
// 解除注册
systemService?.unregisterListener(this)
}
}
计步器
class StepActivity :Activity(),SensorEventListener {
var systemService: SensorManager? = null;
var sensor: Sensor? = null;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_step)
initSensor()
}
private fun initSensor() {
systemService = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensor = systemService!!.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
}
override fun onResume() {
super.onResume()
systemService?.registerListener(this,sensor,1000);
}
override fun onPause() {
super.onPause()
systemService?.unregisterListener(this)
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
}
override fun onSensorChanged(event: SensorEvent?) {
val values = event?.values
step.text = "步数="+values!![0]
}
百度地图
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
override fun onResume() {
super.onResume()
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
bmapView.onResume()
}
override fun onPause() {
super.onPause()
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
bmapView.onPause()
}
override fun onDestroy() {
super.onDestroy()
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
bmapView.onDestroy()
}
}
APP绑定初始化还有两行
仿微信摇一摇(java)
public class WXActivity extends Activity implements SensorEventListener {
private ImageView btn_up;
private ImageView but_down;
private SensorManager sensorManager;
private Sensor sensor;
private Vibrator vibrator;
private SoundPool soundPool;
private TranslateAnimation translateAnimationup;
private TranslateAnimation translateAnimationdown;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wx);
initView();
initData();
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,sensor,200);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
private void initData() {
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
soundPool.load(this,R.raw.wx,1);
translateAnimationup = new TranslateAnimation(0,0,0,-50);
translateAnimationdown = new TranslateAnimation(0,0,0,50);
}
private void initView() {
btn_up = (ImageView) findViewById(R.id.btn_up);
but_down = (ImageView) findViewById(R.id.but_down);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float[] values = sensorEvent.values;
float x = values[0];
float y = values[1];
float z = values[2];
if(Math.abs(x)>15||Math.abs(y)>15||Math.abs(z)>15){
long[] patten = {300,500};
vibrator.vibrate(patten,-1);
soundPool.play(1,1,1,1,1,1);
translateAnimationup.setDuration(2000);
translateAnimationdown.setDuration(2000);
btn_up.startAnimation(translateAnimationup);
but_down.startAnimation(translateAnimationdown);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}