gpt4 book ai didi

android - 在 0 到 135 Android 之间向我们推销时开始 Intent

转载 作者:行者123 更新时间:2023-11-30 02:06:09 27 4
gpt4 key购买 nike

这张图片(取自网络)总结了我想在我的 Android 应用中实现的目标,但我遇到了一些问题。

enter image description here

基本上,当用户将智能手机放在位置 1(底部)时,手机会更改 Activity,而当智能手机位于位置 2 时,另一个 Activity 应该在屏幕中处于 Activity 状态。

这是我用于陀螺仪的类,它运行良好,但如果我尝试在“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!");
}

}
}

};
}

这是我的 list :

<?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_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;

其次,如我所见,您没有启动 Activity,只是试图设置文本。编辑:如果这是您的手机方向指示器,请在正确的位置创建 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 - 在 0 到 135 Android 之间向我们推销时开始 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30633010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com