获得android手机中的传感器信息

这个代码是借鉴了网上的,但是网上的代码有以下错误。

1、出现了sensor cannot be resolved to a type这个错误,如下图。

解决方法是:把红字部分改成<Sensor>.


2、本人是菜鸟,刚刚那个问题也是找了好久的资料才解决的。以为终于没错了,结果把程序部署到真机上,一运行程序就出现闪退。。。

明明eclipse中没有显示出代码错误,这种情况最烦人了,试了好些方法,终于调试成功了。原来是很简单的错误。如下图:


大家看出错误来了么?应该改成</LinearLayout> !!!


下面贴上手机显示结果:



代码如下:

package basic.android.lesson37;

import java.util.List;  

import android.app.Activity;  
import android.content.Context;  
import android.hardware.Sensor;  
import android.hardware.SensorManager;  
import android.os.Bundle;  
import android.widget.TextView;  
   
public class MainActivity extends Activity {  
   
        /** Called when the activity is first created. */ 
        @Override 
        public void onCreate(Bundle savedInstanceState) {  
                super.onCreate(savedInstanceState);  
                setContentView(R.layout.main);  
   
                //准备显示信息的UI组建  
                final TextView tx1 = (TextView) findViewById(R.id.TextView01);  
   
                //从系统服务中获得传感器管理器  
                SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);  
   
                //从传感器管理器中获得全部的传感器列表  
                List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);  
   
                //显示有多少个传感器  
                tx1.setText("经检测该手机有"  + allSensors.size()+"个传感器,他们分别是:\n");  
   
                //显示每个传感器的具体信息  
                for (Sensor s : allSensors) {  
   
                        String tempString = "\n" + "  设备名称:" + s.getName() + "\n" + "  设备版本:" + s.getVersion() + "\n" + "  供应商:" 
                                        + s.getVendor() + "\n";  
   
                        switch (s.getType()) {  
                        case Sensor.TYPE_ACCELEROMETER:  
                                tx1.setText(tx1.getText().toString() + s.getType() + " 加速度传感器accelerometer" + tempString);  
                                break;  
                        case Sensor.TYPE_GYROSCOPE:  
                                tx1.setText(tx1.getText().toString() + s.getType() + " 陀螺仪传感器gyroscope" + tempString);  
                                break;  
                        case Sensor.TYPE_LIGHT:  
                                tx1.setText(tx1.getText().toString() + s.getType() + " 环境光线传感器light" + tempString);  
                                break;  
                        case Sensor.TYPE_MAGNETIC_FIELD:  
                                tx1.setText(tx1.getText().toString() + s.getType() + " 电磁场传感器magnetic field" + tempString);  
                                break;  
                        case Sensor.TYPE_ORIENTATION:  
                                tx1.setText(tx1.getText().toString() + s.getType() + " 方向传感器orientation" + tempString);  
                                break;  
                        case Sensor.TYPE_PRESSURE:  
                                tx1.setText(tx1.getText().toString() + s.getType() + " 压力传感器pressure" + tempString);  
                                break;  
                        case Sensor.TYPE_PROXIMITY:  
                                tx1.setText(tx1.getText().toString() + s.getType() + " 距离传感器proximity" + tempString);  
                                break;  
                        case Sensor.TYPE_TEMPERATURE:  
                                tx1.setText(tx1.getText().toString() + s.getType() + " 温度传感器temperature" + tempString);  
                                break;  
                        default:  
                                tx1.setText(tx1.getText().toString() + s.getType() + " 未知传感器" + tempString);  
                                break;  
                        }  
                }  
   
        }  
        protected void onPause() {
    		
    		super.onPause();
    	}
}  

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
	android:layout_height="fill_parent" 
	android:layout_width="fill_parent" 
	android:text="sds:" 
	android:id="@+id/TextView01"
	 />  	
</LinearLayout> 
从传感器管理器中获取其中某个或者某些传感器的方法有如下三种:
       第一种:获取某种传感器的默认传感器
       Sensor defaultGyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
       第二种:获取某种传感器的列表
       List<Sensor> pressureSensors = sensorManager.getSensorList(Sensor.TYPE_PRESSURE);
       第三种:获取所有传感器的列表,我们这个例子就用的第三种
       List<Sensor> allSensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
欢迎转载。


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值