|
| 1 | +package com.survivingwithandroid.sensor; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import android.hardware.Sensor; |
| 9 | +import android.hardware.SensorEventListener; |
| 10 | +import android.hardware.SensorManager; |
| 11 | +import android.os.Bundle; |
| 12 | +import android.app.Activity; |
| 13 | +import android.app.Service; |
| 14 | +import android.content.Intent; |
| 15 | +import android.view.Menu; |
| 16 | +import android.view.View; |
| 17 | +import android.widget.AdapterView; |
| 18 | +import android.widget.ListView; |
| 19 | +import android.widget.SimpleAdapter; |
| 20 | + |
| 21 | +/* |
| 22 | +* Copyright (C) 2014 Francesco Azzola - Surviving with Android (http://www.survivingwithandroid.com) |
| 23 | +* |
| 24 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 25 | +* you may not use this file except in compliance with the License. |
| 26 | +* You may obtain a copy of the License at |
| 27 | +* |
| 28 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 29 | +* |
| 30 | +* Unless required by applicable law or agreed to in writing, software |
| 31 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 32 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 33 | +* See the License for the specific language governing permissions and |
| 34 | +* limitations under the License. |
| 35 | +*/ |
| 36 | +public class MainActivity extends Activity { |
| 37 | + private SensorManager sensorManager; |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void onCreate(Bundle savedInstanceState) { |
| 41 | + super.onCreate(savedInstanceState); |
| 42 | + setContentView(R.layout.activity_main); |
| 43 | + |
| 44 | + // Get the reference to the sensor manager |
| 45 | + sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE); |
| 46 | + |
| 47 | + // Get the list of sensor |
| 48 | + //List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL); |
| 49 | + |
| 50 | + List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_PRESSURE); |
| 51 | + |
| 52 | + List<Map<String, String>> sensorData = new ArrayList<Map<String,String>>(); |
| 53 | + |
| 54 | + |
| 55 | + for (Sensor sensor: sensorList) { |
| 56 | + Map<String, String> data = new HashMap<String, String>(); |
| 57 | + data.put("name", sensor.getName()); |
| 58 | + data.put("vendor", sensor.getVendor()); |
| 59 | + sensorData.add(data); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + SimpleAdapter sa = new SimpleAdapter(this, sensorData, android.R.layout.simple_list_item_2, new String[]{"name", "vendor"}, new int[]{android.R.id.text1, android.R.id.text2}); |
| 64 | + |
| 65 | + ListView lv = (ListView) findViewById(R.id.sensorList); |
| 66 | + lv.setAdapter(sa); |
| 67 | + |
| 68 | + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onItemClick(AdapterView<?> parent, View view, int pos, |
| 72 | + long id) { |
| 73 | + |
| 74 | + Intent i = new Intent(MainActivity.this, PressActivity.class); |
| 75 | + startActivity(i); |
| 76 | + |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 84 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 85 | + getMenuInflater().inflate(R.menu.main, menu); |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments