獲取手機上存在的感測器
阿新 • • 發佈:2018-11-27
一、修改MainActivity檔案
package com.example.yxp.jxol; import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import java.util.List; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensorlist = sensorManager.getSensorList(Sensor.TYPE_ALL);// 獲取手機上所有的感測器 for (int i = 0; i < sensorlist.size(); i++) { // System.out.println("感測器的總數:"+sensorlist.size()); //System.out.println("感測器"+i+":"+sensorlist.get(i)); Log.d("MainActivity", "感測器的總數:"+sensorlist.size()); Log.d("MainActivity","感測器"+i+":"+sensorlist.get(i)); } } }
二、效果