androidでセンサー一覧を取得
なんか最近スマホの金額が高いので2台持ちにする
今まで使っていたものを通話専用にしてデータ通信をしないようにしてアプリもほとんど消した。
来月あたりでデータ通信量を確認して安く扱えるか確認する
実装
画面
XML
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/scrollView" android:layout_centerVertical="true" android:layout_centerHorizontal="true"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text1" /> </ScrollView> </RelativeLayout>
※スクロールビューで↓を見えるようにする
実装
Activity
package jp.mshige1979.app.samplesensorapp1; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import java.util.List; public class MainActivity extends Activity { private SensorManager manager; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.text1); manager = (SensorManager)getSystemService(SENSOR_SERVICE); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onResume(){ super.onResume(); List<Sensor> sensors = manager.getSensorList(Sensor.TYPE_ALL); int count = 0; String str = ""; for(Sensor s : sensors){ str += s.getName() + ":" + "\n"; count++; } str += "\n"; str += "count=" + count + "\n"; textView.setText(str); } }
結果
所感
実際、検知しているだけで使えるかがよくわかっていないので少しは確認したほうがいいかも