Android enables mobile phone orientation recognition

Source: Internet
Author: User

//自定义导航图标private BitmapDescriptor mIconLocation;//自定义传感器implements SensorEventListenerprivate MyOrientationListener myOrientationListener;//把监听的x方向的值存储到这里private float mCurrentX;
 //initialize icon  miconlocation = Bit        Mapdescriptorfactory.fromresource (R.drawable.ic_launcher); //Initialize callback interface  Myorientationlistener = new  myorient        Ationlistener (Getapplicationcontext ()); //Set callback interface  Myorientationlistener.setonorientationlistener (new  Onorientationlistener () { @Override  public  void    Onorientationchanged  (float  x) {//TODO auto-generated method stub  //callback interface value stored in Mcurrentx  mCu            Rrentx=x; }        });

Synchronization with the life cycle of direction recognition within the activity lifecycle

Start method inside

    //方向识别开始        myOrientationListener.start();      

Stop method inside

//停止方向传感器        myOrientationListener.stop();

Inside the local Data settings

    MyLocationData data = new MyLocationData.Builder()    //设置方向    .direction(mCurrentX)//设置自定义图标        MyLocationConfiguration config = new MyLocationConfiguration(com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL,                         true, mIconLocation);                mBaiduMap.setMyLocationConfigeration(config);

Here is the implementation code for sensor monitoring

 Public  class Myorientationlistener implements Sensoreventlistener{    //used to get Seosor    PrivateSensormanager Msensormanager;PrivateSensor Msensor;PrivateContext Mcontext;Private floatLASTX;PrivateOnorientationlistener Monorientationlistener; Public Myorientationlistener(Context context) {Super(); context= This. Mcontext; }@SuppressWarnings("Deprecation")//How to turn on monitoring     Public void Start() {Msensormanager = (Sensormanager) mcontext.getsystemservice (Context.sensor_service);if(msensormanager!=NULL) {msensor = Msensormanager.getdefaultsensor (sensormanager.sensor_orientation); }if(msensor!=NULL){//Register for monitoringMsensormanager.registerlistener ( This, Msensor, sensormanager.sensor_delay_ui); }    }//How to stop listening     Public void Stop() {Msensormanager.unregisterlistener ( This); }@Override     Public void onaccuracychanged(Sensor arg0,intARG1) {}@Override     Public void onsensorchanged(Sensorevent event) {//Get direction sensor if (Event.sensor.getType () ==sensor.type_orientation) {            floatx = event.values[sensormanager.data_x];//If moving more than one precision then turn            if(Math.Abs (X-LASTX) >1.0){if(monorientationlistener!=NULL) {monorientationlistener.onorientationchanged (x);        }} LASTX =x; }           } Public void Setonorientationlistener(Onorientationlistener Monorientationlistener) { This. Monorientationlistener = Monorientationlistener; }//Define Callback interface     Public  interface onorientationlistener{    //Pass change in the X-direction value of the listener        voidOnorientationchanged (floatx); }}

Android for mobile phone orientation recognition

Related Article

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.