Android Wing of mucchin | related (sensor) Sensor



How to get the list of the (Sensor) sensors that support


Android app from, how to get the list of (sensor) Sensor that are supported by actual equipment?

How to get apps from the Android Sensor, that support, do the following.


Gets the first instance SensorManager.

SensorManager sm = (SensorManager) getSystemService (SENSOR_SERVICE);


The next step is, tentatively, I have two ways, one is turned (deprecated) Deprecated.
But not limited to Android, say in general Java, basically, API has been deprecated and will make sure you do not use.
The first step is to introduce from the intentionally deprecated methods.


Use the () SensorManager.getSensors.

int sensor = sm.getSensors ();

The return value is of type int.
The return value, the value of a standing state flag bit corresponding to each sensor that support will be returned.
For example, whether there is an orientation sensor is measured by an expression such as the following decision.

if ((sensor & SensorManager.SENSOR_ORIENTATION)! = 0) {
/ / If this if statement, orientation sensor are supported
}


The reason the above method has been deprecated, but I do not know, compared to how to be introduced later, and I think I do not very good ease of use.
The following is the recommended method. There is a sense when compared to non-recommended version, it was look like a more object-oriented.




Use the () SensorManager.getSensorList.

List <sensor> list = sm.getSensorList (Sensor.TYPE_ALL);

The return value is of type List Sensor.
Argument, you specify the sensor you want to get, if you want to get the Sensor of all that support, specify the Sensor.TYPE_ALL as described above.
List as follows: When the return value, you can determine if what the sensor is supported.

for (Sensor s: list) {
/ / For example, put a log the name of the sensor
Log.i (s.getName ());
/ / For example, to log the type of sensor issue
Log.i (s.getType ());
}

() return value, but that is the type of Sensor getType.
For example, if a Sensor indicates the orientation sensor, TYPE_ORIENTATION is returned.


If you wish to use the sensor, I'll also registers the listener of the sensor.
Since then, the way that patterns and deprecated above, here are the recommended pattern.
If the first is deprecated.


Use the () SensorManager.registerListener.
If you want to register all sensors are supported, for example, is something like the following.

int sensor = sm.getSensors ();
sm.registerListener (this, sensor);

The first argument specifies the instance that implements the SensorListener.
The second argument is the value of the sum bit of the sensor you want to register.
Actually, this SensorListener interface called itself, has been deprecated.
has been deprecated registerListener (), and also this usage.


The following is the recommended way.
Will use the () SensorManager.registerListener, and the above argument is different.
If you want to register all sensors are supported, for example, is something like the following.

List <sensor> list = sm.getSensorList (Sensor.TYPE_ALL);
for (Sensor s: list) {
sm.registerListener (this, s);
}

The first argument, rather than SensorListener, will be an instance that implements the SensorEventListener.
The second argument specifies the type Sensor instance to be registered.

The event will be notified by () onSensorChanged of an instance that implements the SensorEventListener.
From the arguments, you can get an instance of the type SensorEvent.
So this time, we have up here, on the occasion of another, with respect to event-handling.


This article, when I find out how to get a list of previous sensor (), which had been for the (deprecated), were examined from the API reference is deprecated SensorManager.getSensors.
The sample code that has been published in books and on the net, there may have been fine with (deprecated) Deprecated as of this time.
At that time, if you look at the API reference for the Android, now what has been recommended which API? Please refer to so easy to see that.
The API reference that comes with Android SDK, and see the () SensorManager.getSensors deprecated, has been described as follows.
How to get the list of the (Sensor) sensors that support
This, and you know! "Why should I use (Sensor.TYPE_ALL) getSensorList".


Incidentally, the processing of API reference Ali, please see the following articles.
The location of the Android API Reference