android传感器type_orientation,android – 如何使用SensorManager.getOrientation()而不是TYPE_ORIENTATION...

我在

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

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值