在此图片中(摘自网页)总结了我想在Android应用中实现的功能,但我遇到了一些问题。
基本上,当用户将智能手机置于位置1(底部)时,手机会更改活动,当智能手机位于2时,另一项活动应在屏幕上激活。
这是我用于陀螺仪的类,它运行良好,但如果我尝试在“orient.setText()”之后启动一个Intent它不起作用。 希望有人可以帮助我。
编辑:这是完整的工作代码。
package com.orientation;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import java.util.List;
public class Orientation extends Activity {
private TextView orient;
private Sensor sensor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
orient = (TextView) findViewById(R.id.orient);
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List<android.hardware.Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_GYROSCOPE);
if (sensorList.size() > 0) {
sensor = sensorList.get(0);
} else {
orient.setText("Orientation sensor not present");
}
sensorManager.registerListener(orientationListener, sensor, 0, null);
}
private SensorEventListener orientationListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
float azimuth = sensorEvent.values[0]*180/Math.PI;
float pitch = sensorEvent.values[1]*180/Math.PI;
float roll = sensorEvent.values[2]*180/Math.PI;
if (pitch < -45 && pitch > -135) {
orient.setText("Top side of the phone is Up!");
Intent top = new Intent(Orientation.this, ActivityA.class);
startActivity(top);
} else if (pitch > 0 && pitch < 135) {
orient.setText("Bottom side of the phone is Up!");
Intent bottom= new Intent(Orientation.this, ActivityB.class);
startActivity(bottom);
} else if (roll > 45) {
orient.setText("Right side of the phone is Up!");
} else if (roll < -45) {
orient.setText("Left side of the phone is Up!");
}
}
}
};
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.orientation"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".Orientation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityB"
android:label="@string/title_activity_b" />
<activity
android:name=".ActivityA"
android:label="@string/title_activity_a" >
</activity>
</application>
</manifest>
答案 0 :(得分:1)
首先TYPE_GYROSCOPE返回角度是Radian角度,使用:
float azimuth = sensorEvent.values[0]*180/Math.PI;
float pitch = sensorEvent.values[1]*180/Math.PI;
float roll = sensorEvent.values[2]*180/Math.PI;
其次,
我没有启动Activity
,只是尝试设置文本。
编辑:
如果这是您的电话方向指示器,请在正确的位置制作意图:
package com.orientation;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import java.util.List;
public class Orientation extends Activity {
private TextView orient;
private Sensor sensor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
orient = (TextView) findViewById(R.id.orient);
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List<android.hardware.Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_GYROSCOPE);
if (sensorList.size() > 0) {
sensor = sensorList.get(0);
} else {
orient.setText("Orientation sensor not present");
}
sensorManager.registerListener(orientationListener, sensor, 0, null);
}
private SensorEventListener orientationListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
float azimuth = sensorEvent.values[0]*180/Math.PI;
float pitch = sensorEvent.values[1]*180/Math.PI;
float roll = sensorEvent.values[2]*180/Math.PI;
if (pitch < -45 && pitch > -135) {
orient.setText("Top side of the phone is Up!");
Intent top = new Intent(Orientation.this, <yourTopclass>.class);
startActivity(top);
} else if (pitch > 0 && pitch < 135) {
orient.setText("Bottom side of the phone is Up!");
Intent bottom= new Intent(Orientation.this, <yourBottomclass>.class);
startActivity(bottom);
} else if (roll > 45) {
orient.setText("Right side of the phone is Up!");
} else if (roll < -45) {
orient.setText("Left side of the phone is Up!");
}
}
}
};
}