Android-sensors
Android-センサー
ほとんどのAndroidデバイスには、動き、向き、さまざまな環境条件を測定するセンサーが組み込まれています。 Androidプラットフォームは、3つの広範なカテゴリのセンサーをサポートしています。
- モーションセンサー
- 環境センサー *位置センサー
一部のセンサーはハードウェアベースで、一部はソフトウェアベースのセンサーです。 センサーが何であれ、アンドロイドはこれらのセンサーから生データを取得し、それをアプリケーションで使用することができます。 このアンドロイドはいくつかのクラスを提供します。
Androidは、アプリケーションでセンサーを使用するSensorManagerクラスとSensorクラスを提供します。 センサーを使用するために最初にする必要があるのは、SensorManagerクラスのオブジェクトをインスタンス化することです。 次のようにして達成できます。
SensorManager sMgr;
sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
次に行う必要があるのは、SensorManagerクラスのgetDefaultSensor()メソッドを呼び出して、Sensorクラスのオブジェクトをインスタンス化することです。 その構文は以下のとおりです-
Sensor light;
light = sMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
センサーが宣言されたら、そのリスナーを登録し、onAccuracyChangedとonSensorChangedの2つのメソッドをオーバーライドする必要があります。 その構文は次のとおりです-
sMgr.registerListener(this, light,SensorManager.SENSOR_DELAY_NORMAL);
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
}
サポートされているセンサーのリストを取得する
getSensorListメソッドを呼び出すと、デバイスでサポートされているセンサーのリストを取得できます。getSensorListメソッドは、名前とバージョン番号などの情報を含むセンサーのリストを返します。 その後、リストを繰り返して情報を取得できます。 その構文は以下のとおりです-
sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
List<Sensor> list = sMgr.getSensorList(Sensor.TYPE_ALL);
for(Sensor sensor: list){
}
これらのメソッドとは別に、センサーフレームワークを管理するためのSensorManagerクラスによって提供される他のメソッドがあります。 これらの方法は以下のとおりです-
Sr.No | Method & description |
---|---|
1 |
このメソッドは、指定されたタイプのデフォルトセンサーを取得します。 |
2 |
getInclination(float[] I) このメソッドは、傾斜マトリックスからラジアン単位の地磁気傾斜角を計算します。 |
3 |
registerListener(SensorListener listener, int sensors, int rate) このメソッドは、センサーのリスナーを登録します |
4 |
unregisterListener(SensorEventListener listener, Sensor sensor) このメソッドは、登録されているセンサーのリスナーを登録解除します。 |
5 |
getOrientation(float[] R, float[] values) このメソッドは、回転行列に基づいてデバイスの向きを計算します。 |
6 |
getAltitude(float p0, float p) この方法は、気圧と海面の気圧から高度をメートル単位で計算します。 |
例
以下は、SensorManagerクラスの使用方法を示す例です。 デバイス上のセンサーのリストを表示できる基本的なアプリケーションを作成します。
この例を試すには、実際のデバイスまたはエミュレーターでこれを実行できます。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add necessary code. |
3 | Modify the res/layout/activity_main to add respective XML components. |
4 | Run the application and choose a running android device and install the application on it and verify the results. |
変更された MainActivity.java の内容は次のとおりです。
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.List;
import android.hardware.Sensor;
import android.hardware.SensorManager;
public class MainActivity extends Activity {
TextView tv1=null;
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView2);
tv1.setVisibility(View.GONE);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> mList= mSensorManager.getSensorList(Sensor.TYPE_ALL);
for (int i = 1; i < mList.size(); i++) {
tv1.setVisibility(View.VISIBLE);
tv1.append("\n" + mList.get(i).getName() + "\n" + mList.get(i).getVendor() + "\n" + mList.get(i).getVersion());
}
}
@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);
}
}
以下は、xml activity_main.xml の変更されたコンテンツです。
'_以下のコードでは、 abc はfinddevguides.comのロゴについて示しています_
<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"
android:transitionGroup="true">
<TextView android:text="Sensor " android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:theme="@style/Base.TextAppearance.AppCompat"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"
android:layout_below="@+id/imageView"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
以下は res/values/string.xml の内容です。
<resources>
<string name="app_name">My Application</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
以下は AndroidManifest.xml ファイルの内容です。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
変更したばかりのアプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
デバイスの画面を見ると、デバイスでサポートされているセンサーのリストが、名前とバージョン、その他の情報とともに表示されます。
このアプリケーションを異なるデバイスで実行する場合、出力はデバイスでサポートされているセンサーの数に依存するため、出力は異なります。