i implement sensoreventlistener on shake changes ringer mode. using service can change ringer mode outside activity whenever like. code service 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 communication return ibinder implementation return null; } }
when open app 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 create service com.nadeem.shakeit.service: java.lang.nullpointerexception 07-15 12:04:02.800: e/androidruntime(22092): @ android.app.activitythread.handlecreateservice(activitythread.java:2076) 07-15 12:04:02.800: e/androidruntime(22092): @ android.app.activitythread.access$2500(activitythread.java:123) 07-15 12:04:02.800: e/androidruntime(22092): @ android.app.activitythread$h.handlemessage(activitythread.java:993) 07-15 12:04:02.800: e/androidruntime(22092): @ android.os.handler.dispatchmessage(handler.java:99) 07-15 12:04:02.800: e/androidruntime(22092): @ android.os.looper.loop(looper.java:130) 07-15 12:04:02.800: e/androidruntime(22092): @ android.app.activitythread.main(activitythread.java:3835) 07-15 12:04:02.800: e/androidruntime(22092): @ java.lang.reflect.method.invokenative(native method) 07-15 12:04:02.800: e/androidruntime(22092): @ java.lang.reflect.method.invoke(method.java:507) 07-15 12:04:02.800: e/androidruntime(22092): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:858) 07-15 12:04:02.800: e/androidruntime(22092): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 07-15 12:04:02.800: e/androidruntime(22092): @ 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): @ com.nadeem.shakeit.service.oncreate(service.java:21) 07-15 12:04:02.800: e/androidruntime(22092): @ android.app.activitythread.handlecreateservice(activitythread.java:2066) 07-15 12:04:02.800: e/androidruntime(22092): ... 10 more
edit-after instantiating sensormanager again getting nullpointerexception. 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): @ com.nadeem.shakeit.service.getaccelerometer(service.java:64) 07-15 12:35:45.280: e/androidruntime(22469): @ com.nadeem.shakeit.service.onsensorchanged(service.java:43) 07-15 12:35:45.280: e/androidruntime(22469): @ android.hardware.sensormanager$listenerdelegate$1.handlemessage(sensormanager.java:529) 07-15 12:35:45.280: e/androidruntime(22469): @ android.os.handler.dispatchmessage(handler.java:99) 07-15 12:35:45.280: e/androidruntime(22469): @ android.os.looper.loop(looper.java:130) 07-15 12:35:45.280: e/androidruntime(22469): @ android.app.activitythread.main(activitythread.java:3835) 07-15 12:35:45.280: e/androidruntime(22469): @ java.lang.reflect.method.invokenative(native method) 07-15 12:35:45.280: e/androidruntime(22469): @ java.lang.reflect.method.invoke(method.java:507) 07-15 12:35:45.280: e/androidruntime(22469): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:858) 07-15 12:35:45.280: e/androidruntime(22469): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 07-15 12:35:45.280: e/androidruntime(22469): @ dalvik.system.nativestart.main(native method)
sensormanager
uninitiallised. missing in oncreate()
sensormanager=(sensormanager) getsystemservice(...);
Comments
Post a Comment