zoukankan      html  css  js  c++  java
  • [置顶] Android代码传感器方向传感

    一个简单的小Demo测试手机方向传感:

    具体代码如下:


    [Java代码]Sample.java

    package com.example.test_sensor_orientation;
    
    import org.openintents.sensorsimulator.hardware.Sensor;
    import android.app.Activity;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public  class Sample extends Activity implements  android.hardware.SensorEventListener {
    	
    	TextView myTextView1;
    	TextView myTextView2;
    	TextView myTextView3;
    	
    	private SensorManager mySensorManager;
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		myTextView1 = (TextView) findViewById(R.id.myTextView1);
    		myTextView2 = (TextView) findViewById(R.id.myTextView2);
    		myTextView3 = (TextView) findViewById(R.id.myTextView3);
    		mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    
    	}
    
    	@Override
    	protected void onResume() {
    		mySensorManager.registerListener(
    				this,
    				mySensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
    				SensorManager.SENSOR_DELAY_GAME
    				);
    		super.onResume();
    	}
    
    	@Override
    	protected void onStop() {
    		// TODO Auto-generated method stub
    		mySensorManager.unregisterListener(this);
    		super.onStop();
    	}
    	@Override
    	protected void onPause() {
    		mySensorManager.unregisterListener(this);
    		super.onPause();
    	}
    
    	@Override
    	public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void onSensorChanged(android.hardware.SensorEvent event) {
    		// TODO Auto-generated method stub
    		float[] values = event.values;
    		int sensorType = event.sensor.TYPE_ORIENTATION;
    		if (sensorType == Sensor.TYPE_ORIENTATION) {
    			myTextView1.setText("Yaw为:"+values[0]); 
    			myTextView2.setText("Pitch为:"+values[1]); 
    			myTextView3.setText("Roll为:"+values[2]); 
    		}
    	}
    
    }
    

    [XML代码]main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/myTextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    	 />
        <TextView
            android:id="@+id/myTextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    	 />
        <TextView
            android:id="@+id/myTextView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    	 />
    
    </LinearLayout>
    

    String.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
    <string name="hello">Hello World, Sample</string> 
    <string name="app_name">Sample</string> 
    <string name="title">方向传感器</string> 
    <string name="myTextView1">Yaw为:</string> 
    <string name="myTextView2">Pitch为:</string> 
    <string name="myTextView3">Roll为:</string> 
    </resources> 



  • 相关阅读:
    Linux进程管理工具Supervisor
    RSA加密传输代码示例
    静态网站创建工具Docusaurus
    Proactor和Reactor模型
    机器学习中的七宗罪
    Tokio internals: Understanding Rust's asynchronous I/O framework from the bottom up
    开源软件创建SOC的一份清单
    How to setup SOC using open-source tools
    彼得定律
    深入浅出通信原理连载
  • 原文地址:https://www.cnblogs.com/anjon520/p/3249940.html
Copyright © 2011-2022 走看看