我实现了一个可以改变铃声模式的sensorEventListener。我正在使用一项服务,以便我可以随时在活动之外更改振铃模式。这是我正在使用的服务的代码 -
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;
}
}
当我打开应用程序时,它会强行关闭。 Logcat显示: -
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
编辑 - 再次实例化sensorManager后,我得到一个nullPointerException。堆栈跟踪是 -
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)
答案 0 :(得分:6)
sensorManager
未初始化。您在onCreate()
sensorManager=(SensorManager) getSystemService(...);
答案 1 :(得分:2)
初始化您的sensorManager变量。
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
答案 2 :(得分:1)
您刚刚声明了SensorManager
引用变量,目前它包含null
引用,您在哪里实例化它?
private SensorManager sensorManager;
调用此null
引用上的任何方法都会引发NullPointerException
:
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
您需要在onCreate()
中初始化它:
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
即使您的AudioManager am;
引用null
,您也需要在调用任何方法之前对其进行初始化。