[Android] Sensor-accelerometer -1[turn]

Source: Internet
Author: User

/* Copyright (C) The Rexsee Open Source Project * * Licensed under the Rexsee License, Version 1.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.rexsee.com/CN/legal/license.html * * unless required by applicabl E law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warr Anties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */Package rexsee.sensor; ImportRexsee.core.browser.JavascriptInterface; Need two projects to outsource, need to download importRexsee.core.browser.RexseeBrowser; Specific reference http://www.rexsee.com/CN/helpReference.php   Import Android.content.Context; Import Android.hardware.Sensor; Import android.hardware.SensorEvent; Import Android.hardware.SensorEventListener; Import Android.hardware.SensorManager; public class Rexseesensoraccelerometer implements Javascriptinterface {private static final String interface_name = "Acce Lerometer "; @Override public String Getinterfacename () {return mBrowser.application.resources.prefix + interface_name;} @Override P Ublic javascriptinterface getinheritinterface (Rexseebrowser childbrowser) {return this;} @Override public Javascriptinterface getnewinterface (Rexseebrowser childbrowser) {return new Rexseesensoraccelerometer (ChildBrowser) ; public static final String event_onaccelerate = "onaccelerometerchanged"; Private final Context Mcontext; Private final Rexseebrowser Mbrowser; Private final Sensormanager Msensormanager; Private final Sensoreventlistener Msensorlistener; Private final Sensor Msensor; private int mrate = Sensormanager.sensor_delay_normal; Private int mcycle = 100; private int meventcycle = 100; private float maccuracyx = 0; private float maccuracyy = 0; private float Maccuracyz = 0; Private Long lastupdate =-1; Private Long lastevent =-1; private float x = -999, y = -999, z =-999;  PublicRexseesensoraccelerometer(Rexseebrowser browser) {mcontext = Browser.getcontext (); mbrowser = browser; Browser.eventList.add (event_onaccelerate);Msensormanager= (Sensormanager) mcontext.getsystemservice (Context.sensor_service);Msensor= Msensormanager.getdefaultsensor (Sensor.type_accelerometer); Msensorlistener = newSensoreventlistener() {@Override public void onaccuracychanged (sensor sensor, int. accuracy) {} @Override public voidonsensorchanged(Sensorevent event) {if (Event.sensor.getType ()! = Sensor.type_accelerometer) return; Long curtime = System.currenttimemillis (); if (lastupd ate = =-1 | | (curtime-lastupdate) > mcycle) {lastupdate = Curtime; float lastx = x; float lasty = y; float lastz = z; x = event.v ALUES[SENSORMANAGER.DATA_X]; y = event.values[sensormanager.data_y]; z = event.values[sensormanager.data_z]; if (lastevent = =-1 | | (curtime-lastevent) > Meventcycle) {if (maccuracyx >= 0 && math.abs (x-lastx) > maccuracyx) | | (maccuracyy >= 0 && math.abs (y-lasty) > Maccuracyy) | | (maccuracyz >= 0 && math.abs (z-lastz) > Maccuracyz)) {lastevent = Curtime; MBrowser.eventList.run (event_onaccelerate);} } } } }; } public String Getlastknownx () {return (x = =-999)? "Null": String.valueof (x); } public String Getlastknowny () {return (y = =-999)? "Null": string.valueof (y); } public String Getlastknownz () {return (z = =-999)? "Null": String.valueof (z); } public void Setrate (String rate) {mrate = Sensorrate.getint (rate),} public String getrate () {return sensorrate.getstring (Mrat e); } public void setcycle (int milliseconds) {mcycle = milliseconds,} public int getcycle () {return mcycle;} public void S eteventcycle (int milliseconds) {meventcycle = milliseconds;} public int geteventcycle () {return meventcycle;} public v  OID setaccuracy (float x, float y, float z) {maccuracyx = x; maccuracyy = y; maccuracyz = z;} public float getaccuracyx () {return maccuracyx;} public float Getaccuracyy () {return maccuracyy,} public float Getaccuracyz () {return Maccuracyz ; } public boolean IsReady () {return (Msensor = = null)? False:true,} public void Start () {if (IsReady ()) {///If the sensor is available, then Registered Listener Msensormanager.registerlistener (Msensorlistener, Msensor, mrate); } else {//Otherwise, report an exception mbrowser.exception (Getinterfacename (), "Accelerometer is not found.");} public void Stop () {if (IsReady ()) {//Logoff listener Msensormanager.unregisterlistener (Msensorlistener);} } } 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.