Reputation: 459
I want to check if an Android device has an accelerometer or not so that I can put a toast message saying that the game won't work properly on the current device if there isn't an accelerometer. I am looking for something like this:
if(DEVICE_HAS_ACCELEROMETER == false){
Toast.makeText(context,"No Accelerometer Found.",Toast.LENGTH_LONG).show();
}
If you can fill in the DEVICE_HAS_ACCELEROMETER part, that would do the job.
PS: I am not in an activity.
Upvotes: 7
Views: 6312
Reputation: 267614
You can also check it via PackageManger in one line.
boolean isAccSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER);
Upvotes: 0
Reputation: 7439
SensorManager sManager;
acce = sManager.registerListener(this,sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if(acce)
{
}
Upvotes: 0
Reputation: 18151
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (accelerometerSensor != null)
{
// Device has Accelerometer
}
Upvotes: 13
Reputation: 8251
Do something like the following ,
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> s=sensorManager.getSensorList(Sensor.TYPE_ALL);
for (int i=0;i<s.size();i++)
{
Sensor tmp = s.get(i);
if (tmp.getType() == Sensor.TYPE_ACCELEROMETER)
DEVICE_HAS_ACCELEROMETER=true;
else
DEVICE_HAS_ACCELEROMETER=false;
}
Upvotes: 3
Reputation: 12181
This will work:
SensorManager Sensors = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor mAccelerometer = Sensors.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccelerometer will have the accelerometre val.
Upvotes: 1
Reputation: 154
accelerometer = sensorMgr.registerListener(this,sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if(accelerometer)
{
}
Upvotes: 0
Reputation: 11194
boolean accelerometer;
accelerometer = sensorMgr.registerListener(this,sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if(accelerometer)
{
.
.
}
Upvotes: 3