Start intent when pitch us between 0 and 135 Android -
in image (taken web) summarizes want achieve in android app, have problems.

basically when user has smartphone in position 1 (bottom) phone change activity, , when smartphone position in 2, activity should active in screen.
this class use gyroscope , works good, if try start intent after "orient.settext()" doesn't work. hope can me.
edit: full working code.
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 phone up!"); intent top = new intent(orientation.this, activitya.class); startactivity(top); } else if (pitch > 0 && pitch < 135) { orient.settext("bottom side of phone up!"); intent bottom= new intent(orientation.this, activityb.class); startactivity(bottom); } else if (roll > 45) { orient.settext("right side of phone up!"); } else if (roll < -45) { orient.settext("left side of phone up!"); } } } }; } this manifest:
<?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>
first of type_gyroscope return angles radian angles, use:
float azimuth = sensorevent.values[0]*180/math.pi; float pitch = sensorevent.values[1]*180/math.pi; float roll = sensorevent.values[2]*180/math.pi; second, not start activity can see, trying set text. edit: if indicator phone's orientation , make intent @ right place:
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 phone up!"); intent top = new intent(orientation.this, <yourtopclass>.class); startactivity(top); } else if (pitch > 0 && pitch < 135) { orient.settext("bottom side of phone up!"); intent bottom= new intent(orientation.this, <yourbottomclass>.class); startactivity(bottom); } else if (roll > 45) { orient.settext("right side of phone up!"); } else if (roll < -45) { orient.settext("left side of phone up!"); } } } }; }
Comments
Post a Comment