- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在用 android 开发一个应用程序。该应用程序在数据库中保存了一些引号,我想使用手机的传感器在屏幕上滑动引号。所以我希望当手机向左移动时,我可以在屏幕上看到下一个报价,当我将手机向右移动时,我可以在屏幕上看到上一个报价。你觉得什么类型的感应手机比较好用?我正在尝试使用 Type_orientation 但出现错误:此常量在 API 级别 8 中已弃用。如何将其替换为 SensorManager.getOrientation() 。或者您认为还有其他传感器更适合我使用吗?
package com.example.prova1;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class SlideQuote extends Activity implements SensorEventListener
{
//a TextView
private TextView quote;
private TextView author;
//the Sensor Manager
private SensorManager sManager;
float x;
int id;
int total;
String s1,s2;
Database quotedatabase = new Database(this);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.slide_quote);
//get the TextView from the layout file
quote = (TextView) findViewById(R.id.textView3);
author = (TextView) findViewById(R.id.textView4);
id=1;
//get a hook to the sensor service
sManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sManager.getSensorList(Sensor.TYPE_ORIENTATION).size()!=0){
Sensor s =sManager.getSensorList(Sensor.TYPE_ORIENTATION).get(o);
sManager.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
s1= quotedatabase.getQuote(id);
s2= quotedatabase.getAuthor(id);
total=quotedatabase.getQuotesCount();
quote.setText(s1);
author.setText(s2);
}
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
/*register the sensor listener to listen to the gyroscope sensor, use the
callbacks defined in this class, and gather the sensor information as quick
as possible*/
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_NORMAL);
}
//When this Activity isn't visible anymore
@Override
protected void onStop()
{
//unregister the sensor listener
sManager.unregisterListener(this);
super.onStop();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing.
}
@Override
public void onSensorChanged(SensorEvent event)
{
//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
//else it will output the Roll, Pitch and Yawn values
x=event.values[2];
if(x>25){
if(id==total)
{id=1;
}
else{
id++;
}
s1= quotedatabase.getQuote(id);
s2= quotedatabase.getAuthor(id);
quote.setText(s1);
author.setText(s2);
}
if(x<-25){
if(id==1)
{id=total;}
else{
id--;}
s1= quotedatabase.getQuote(id);
s2= quotedatabase.getAuthor(id);
quote.setText(s1);
author.setText(s2);
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
sManager.unregisterListener(this);
Intent backIntent = new Intent(getApplication(), Quote.class);
finish();
startActivity(backIntent);
}
}
最佳答案
Android 运动传感器页面提供了一些使用传感器的示例:
对于陀螺仪你可以使用下面的代码:
// Create a constant to convert nanoseconds to seconds.
private static final float NS2S = 1.0f / 1000000000.0f;
private final float[] deltaRotationVector = new float[4]();
private float timestamp;
public void onSensorChanged(SensorEvent event) {
// This timestep's delta rotation to be multiplied by the current rotation
// after computing it from the gyro sample data.
if (timestamp != 0) {
final float dT = (event.timestamp - timestamp) * NS2S;
// Axis of the rotation sample, not normalized yet.
float axisX = event.values[0];
float axisY = event.values[1];
float axisZ = event.values[2];
// Calculate the angular speed of the sample
float omegaMagnitude = sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
// Normalize the rotation vector if it's big enough to get the axis
// (that is, EPSILON should represent your maximum allowable margin of error)
if (omegaMagnitude > EPSILON) {
axisX /= omegaMagnitude;
axisY /= omegaMagnitude;
axisZ /= omegaMagnitude;
}
// Integrate around this axis with the angular speed by the timestep
// in order to get a delta rotation from this sample over the timestep
// We will convert this axis-angle representation of the delta rotation
// into a quaternion before turning it into the rotation matrix.
float thetaOverTwo = omegaMagnitude * dT / 2.0f;
float sinThetaOverTwo = sin(thetaOverTwo);
float cosThetaOverTwo = cos(thetaOverTwo);
deltaRotationVector[0] = sinThetaOverTwo * axisX;
deltaRotationVector[1] = sinThetaOverTwo * axisY;
deltaRotationVector[2] = sinThetaOverTwo * axisZ;
deltaRotationVector[3] = cosThetaOverTwo;
}
timestamp = event.timestamp;
float[] deltaRotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
// User code should concatenate the delta rotation we computed with the current rotation
// in order to get the updated rotation.
// rotationCurrent = rotationCurrent * deltaRotationMatrix;
}
}
关于android - 如何使用 SensorManager.getOrientation() 而不是 TYPE_ORIENTATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19077850/
要在 Android 中从欧拉角(例如俯仰、滚转、方位角)中获取方向,需要执行以下操作: SensorManager.getRotationMatrix(float[] R, float[] I, f
API 演示 -> 图形 -> Compass 它只能正常工作,直到您不更改设备的自然方向。在大多数手机中是纵向的,而大多数 10 英寸平板电脑是横向的。如果您进行更改,则需要将其旋转 90 度。我希
我的线程上有此命令,但它不起作用:sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 我想在不打开我的应用程序的情况下创
我希望实现一个 SensorManager 类,它可以处理所有与传感器相关的事情,但这显然需要我启动一个新 Activity 。 我不打算这样做 - 只是运行计算。是使用服务处理此问题的最佳方式,您可
我正在为平板电脑开发一个应用程序,平板电脑将放在一个围绕给定点旋转的支架上。这意味着当应用程序首次打开时,很少从平板电脑移动。该应用程序将根据平板电脑在支架上的旋转显示各种内容。无论我多么努力地搜索,
在我的应用中,我使用光线和接近传感器来检测手机自掏腰包功能,然后在检测完成后注销 SensorManager。但是,即使应用程序的 CPU 使用率仅显示 1-2 秒,电池使用率始终显示我的应用程序为否
我的应用程序创建了一个 Service,它覆盖了 onStartCommand 以创建一个 SensorManager 并将一个 SensorEventListener 附加到其中一个传感器: pub
我需要检索手机的方向。目前我写了这个: public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType(
我正在为我的 Android 应用程序使用传感器。我用一行代码注册传感器: mySensorManager.registerListener(this, orientationSensor, Sens
我在以下的帮助下获得了我的手机方向 SensorManager.getOrientation 但是结果很不稳定,大概+-8度,有什么好的过滤结果的方法吗? 这是我获取值的方式: public void
我有一个为 Cordova 应用程序编写的 Android 后台服务,我想通过 sensorManager 与加速度计进行交互。 我觉得我已经很接近了,但我认为还不够。 出于某种原因,sensorMa
我在 logcat 中遇到传感器管理器和错误的问题。当我用这一行注册监听器时: sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(
我在资源中找到了这个值。 /** Standard gravity (g) on Earth. This value is equivalent to 1G */ public static
我一直在研究如何在我的 Android 应用程序中读取步数,并在文档中找到了两种完全不同的方法: Using SensorManager : val sensorManager = getSystem
我使用单例模式实现了一个简单的指南针类。一旦我向传感器管理器注册了加速度计和磁力计监听器,内存消耗就会增加。我的 GPS 类与指南针类非常相似,并且我没有出现内存泄漏问题。我在 onSensorCha
@Override protected void onResume(){ //super.onResume(); sensorManager.registerListener
Scala/Android 新手问题。我正在尝试将这个简单的代码从 Java 重写为 Scala。Java 工作代码: private final SensorEventListener mAccLi
我的源代码是: import android.content.Context; import android.hardware.SensorManager; public class ShakeEve
我在三星 S6 上使用心率传感器。为了访问此传感器,从 SDK 版本 > 23 开始,用户必须授予 BODY_SENSORS 权限 https://stackoverflow.com/a/326360
我使用 StackOverflow 的一些建议编写了一个加速度计应用程序(用于学习目的)。一切正常,但我在代码中收到“SensorManager.DATA_X 已弃用”消息作为警告: // setup
我是一名优秀的程序员,十分优秀!