I implement a sensorEventListener which on shake changes ringer mode. I am using a service so that i can change the ringer mode from outside the activity whenever i like. This is my code for the service i am using-
package com.nadeem.shakeit;
import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.IBinder;
public class service extends Service implements SensorEventListener {
private SensorManager sensorManager;
private long lastUpdate;
AudioManager am;
@Override
public void onCreate() {
super.onCreate();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
/*@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
return Service.START_REDELIVER_INTENT;
}*/
@Override
public void onDestroy() {
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event) {
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
if (accelationSquareRoot >= 5) //
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
int ringerMode = am.getRingerMode();
if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
} else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else {
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public IBinder onBind(Intent intent) {
// TODO for communication return IBinder implementation
return null;
}
}
when i open the app it force closes. Logcat shows:-
07-15 12:04:02.800: E/AndroidRuntime(22092): FATAL EXCEPTION: main
07-15 12:04:02.800: E/AndroidRuntime(22092): java.lang.RuntimeException: Unable to create service com.nadeem.shakeit.service: java.lang.NullPointerException
07-15 12:04:02.800: E/AndroidRuntime(22092): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2076)
07-15 12:04:02.800: E/AndroidRuntime(22092): at android.app.ActivityThread.access$2500(ActivityThread.java:123)
07-15 12:04:02.800: E/AndroidRuntime(22092): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
07-15 12:04:02.800: E/AndroidRuntime(22092): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 12:04:02.800: E/AndroidRuntime(22092): at android.os.Looper.loop(Looper.java:130)
07-15 12:04:02.800: E/AndroidRuntime(22092): at android.app.ActivityThread.main(ActivityThread.java:3835)
07-15 12:04:02.800: E/AndroidRuntime(22092): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 12:04:02.800: E/AndroidRuntime(22092): at java.lang.reflect.Method.invoke(Method.java:507)
07-15 12:04:02.800: E/AndroidRuntime(22092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-15 12:04:02.800: E/AndroidRuntime(22092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-15 12:04:02.800: E/AndroidRuntime(22092): at dalvik.system.NativeStart.main(Native Method)
07-15 12:04:02.800: E/AndroidRuntime(22092): Caused by: java.lang.NullPointerException
07-15 12:04:02.800: E/AndroidRuntime(22092): at com.nadeem.shakeit.service.onCreate(service.java:21)
07-15 12:04:02.800: E/AndroidRuntime(22092): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2066)
07-15 12:04:02.800: E/AndroidRuntime(22092): ... 10 more
Edit-After instantiating the sensorManager again i am getting a nullPointerException. The stack trace is-
07-15 12:35:45.280: E/AndroidRuntime(22469): FATAL EXCEPTION: main
07-15 12:35:45.280: E/AndroidRuntime(22469): java.lang.NullPointerException
07-15 12:35:45.280: E/AndroidRuntime(22469): at com.nadeem.shakeit.service.getAccelerometer(service.java:64)
07-15 12:35:45.280: E/AndroidRuntime(22469): at com.nadeem.shakeit.service.onSensorChanged(service.java:43)
07-15 12:35:45.280: E/AndroidRuntime(22469): at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:529)
07-15 12:35:45.280: E/AndroidRuntime(22469): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 12:35:45.280: E/AndroidRuntime(22469): at android.os.Looper.loop(Looper.java:130)
07-15 12:35:45.280: E/AndroidRuntime(22469): at android.app.ActivityThread.main(ActivityThread.java:3835)
07-15 12:35:45.280: E/AndroidRuntime(22469): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 12:35:45.280: E/AndroidRuntime(22469): at java.lang.reflect.Method.invoke(Method.java:507)
07-15 12:35:45.280: E/AndroidRuntime(22469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-15 12:35:45.280: E/AndroidRuntime(22469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-15 12:35:45.280: E/AndroidRuntime(22469): at dalvik.system.NativeStart.main(Native Method)
sensorManager
is uninitiallised. You are missing something like this in your onCreate()
sensorManager=(SensorManager) getSystemService(...);