亲宝软件园·资讯

展开

微信的摇一摇 android 类似微信的摇一摇功能实现思路及代码

人气:0
想了解android 类似微信的摇一摇功能实现思路及代码的相关内容吗,在本文为您仔细讲解微信的摇一摇的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:微信,摇一摇,下面大家一起来学习吧。
复制代码 代码如下:

package com.eboy.testyaoyiyao;
import java.text.SimpleDateFormat;
import java.util.Date;
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.os.Vibrator;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
//Sensor管理器
private SensorManager mSensorManager = null;
//震动
private Vibrator mVibrator = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent arg0) {
// TODO Auto-generated method stub
int sensorType = arg0.sensor.getType();
float[] values = arg0.values;
if (sensorType == Sensor.TYPE_ACCELEROMETER){
if (Math.abs(values[0]) > 14 || Math.abs(values[1]) > 14 || Math.abs(values[2]) > 14){
mVibrator.vibrate(100);
TextView tv1 = (TextView)findViewById(R.id.textView1);
SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
tv1.setText(f.format(new Date()) + "手机摇动了...");
}
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
mSensorManager.unregisterListener(this);
super.onStop();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
mSensorManager.unregisterListener(this);
super.onPause();
}
}

加载全部内容

相关教程
猜你喜欢
用户评论