当我们在 0 到 135 之间时开始意图 Android
Start intent when pitch us between 0 and 135 Android
这张图片(取自网络)总结了我想在我的 Android 应用程序中实现的目标,但我遇到了一些问题。
基本上当用户在位置1(底部)有smartphone时phone改变activity,当smartphone位置在2时,另一个 activity 应该在屏幕上处于活动状态。
这是我用于陀螺仪的 class,它运行良好,但如果我尝试在 "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>
首先TYPE_GYROSCOPEreturn角是弧度角,用:
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
,只是试图设置文本。
编辑:
如果那是 phone 方向的指标,请在正确的位置创建 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, <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!");
}
}
}
};
}
这张图片(取自网络)总结了我想在我的 Android 应用程序中实现的目标,但我遇到了一些问题。
基本上当用户在位置1(底部)有smartphone时phone改变activity,当smartphone位置在2时,另一个 activity 应该在屏幕上处于活动状态。
这是我用于陀螺仪的 class,它运行良好,但如果我尝试在 "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>
首先TYPE_GYROSCOPEreturn角是弧度角,用:
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
,只是试图设置文本。
编辑:
如果那是 phone 方向的指标,请在正确的位置创建 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, <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!");
}
}
}
};
}