位置: 编程技术 - 正文
推荐整理分享android 实现摇一摇功能(android摇一摇实现),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:安卓系统摇一摇在哪里,android摇一摇实现,android摇一摇实现,安卓手机摇一摇没反应,android摇一摇实现,安卓系统摇一摇在哪里,android 摇一摇,安卓手机怎么摇一摇,内容如对您有帮助,希望把文章链接给更多的朋友!
实现“摇一摇”功能,其实很简单,就是检测手机的重力感应,具体实现代码如下:
一、在 AndroidManifest.xml 中添加操作权限
<uses-permission android:name="android.permission.VIBRATE"/>
二、实现代码
[java] view plaincopypackage com.xs.test; 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.Handler; import android.os.Message; import android.os.Vibrator; import android.util.Log; import android.widget.Toast; /** * 安卓晃动手机监听--“摇一摇” * * @author 单红宇 * */ public class TestSensorActivity extends Activity { private SensorManager sensorManager; private Vibrator vibrator; private static final String TAG = "TestSensorActivity"; private static final int SENSOR_SHAKE = ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); } @Override protected void onResume() { super.onResume(); if (sensorManager != null) {// 注册监听器 sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); // 第一个参数是Listener,第二个参数是所得传感器类型,第三个参数获取传感器信息的频率 } } @Override protected void onPause() { super.onPause(); if (sensorManager != null) {// 取消监听器 sensorManager.unregisterListener(sensorEventListener); } } /** * 重力感应监听 */ private SensorEventListener sensorEventListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { // 传感器信息改变时执行该方法 float[] values = event.values; float x = values[0]; // x轴方向的重力加速度,向右为正 float y = values[1]; // y轴方向的重力加速度,向前为正 float z = values[2]; // z轴方向的重力加速度,向上为正 Log.i(TAG, "x轴方向的重力加速度" x ";y轴方向的重力加速度" y ";z轴方向的重力加速度" z); // 一般在这三个方向的重力加速度达到就达到了摇晃手机的状态。 int medumValue = ;// 三星 i怎么晃都不会超过,没办法,只设置了 if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) { vibrator.vibrate(); Message msg = new Message(); msg.what = SENSOR_SHAKE; handler.sendMessage(msg); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; /** * 动作执行 */ Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SENSOR_SHAKE: Toast.makeText(TestSensorActivity.this, "检测到摇晃,执行操作!", Toast.LENGTH_SHORT).show(); Log.i(TAG, "检测到摇晃,执行操作!"); break; } } }; } 这种摇一摇比较普通,没有经过算法的加强,重力加速度就是。算法优化如下package com.example.url;import java.io.IOException;import java.net.URL;import com.hahashijie.imageload.ImageLoader;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.Drawable;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.Vibrator;import android.util.Log;import android.widget.ImageView;@SuppressLint("HandlerLeak")public class MainActivity extends Activity {private ImageView image;private SensorManager sensorManager;private Sensor sensor;private Vibrator vibrator;private static final int UPTATE_INTERVAL_TIME = ;private static final int SPEED_SHRESHOLD = ;//这个调节灵敏度private long lastUpdateTime;private float lastX;private float lastY;private float lastZ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);image = (ImageView) findViewById(R.id.image);}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();if (sensorManager != null) { sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } if (sensor != null) { sensorManager.registerListener(sensorEventListener, sensor,SensorManager.SENSOR_DELAY_GAME);//这里选择感应频率 }}/*** 重力感应监听*/private SensorEventListener sensorEventListener = new SensorEventListener() {@Overridepublic void onSensorChanged(SensorEvent event) {long currentUpdateTime = System.currentTimeMillis();long timeInterval = currentUpdateTime - lastUpdateTime;if (timeInterval < UPTATE_INTERVAL_TIME) {return;}lastUpdateTime = currentUpdateTime;// 传感器信息改变时执行该方法float[] values = event.values;float x = values[0]; // x轴方向的重力加速度,向右为正float y = values[1]; // y轴方向的重力加速度,向前为正float z = values[2]; // z轴方向的重力加速度,向上为正float deltaX = x - lastX;float deltaY = y - lastY;float deltaZ = z - lastZ;lastX = x;lastY = y;lastZ = z;double speed = (Math.sqrt(deltaX * deltaX deltaY * deltaY deltaZ * deltaZ) / timeInterval) * ;if (speed >= SPEED_SHRESHOLD) {vibrator.vibrate();image.setImageResource(R.drawable.running);}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}};}Android基础系列-----------Android系统框架 Android系统是有众多的子项目组成的。从编程语言角度来看,这些项目主要是使用Java和C/C来实现的;从整体系统框架而言,分成内核层、硬件抽象层、系
Android广播接收器Broadcast Receiver-android学习之旅(十二) 首先继承BroadcastReceiver类,并在manifest中注册publicclassMyReceiverextendsBroadcastReceiver{publicMyReceiver(){}@OverridepublicvoidonReceive(Contextcontext,Intentintent){thrownewUnsupporte
android实现拨打电话但不弹出拨号界面 这里只提供一个大概的思路,整个流程不是我一个人就能完成的。测试机型为htconex,Android版本4.2.2,已ROOT。要实现拨号程序可能很简单,一个简单的Int
标签: android摇一摇实现
本文链接地址:https://www.jiuchutong.com/biancheng/381025.html 转载请保留说明!友情链接: 武汉网站建设