main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>SensorActivity
public class SensorActivity extends Activity {
private static final String TAG = "SensorActivity";
private SensorManager mgr;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.insert2Tv("onCreate...");
// Android 所有的感應器的統一介面
this.mgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
// 取得所有感應器
List<Sensor> list = this.mgr.getSensorList(Sensor.TYPE_ALL);
if (list.isEmpty()) {
this.insert2Tv("不支援任何感應器");
}
else {
for (Sensor s : list) {
this.insert2Tv("支援的感應器:" + s.getName());
}
}
}
private void insert2Tv(String msg) {
if (this.tv == null) {
this.tv = (TextView) this.findViewById(R.id.tv);
}
this.tv.setText(this.tv.getText().toString() + "\n" + msg);
Log.d(SensorActivity.TAG, msg);
}
}感應器要安裝到手機上才會運作。

沒有留言:
張貼留言