i need run service (always in background, want service checking if application closed) control proximity sensor status, , when it's covered launch activity. tried had error. examples of this?
p.s. in androidmanifest added:
<service android:name="xxx.xxxxx.xxxxx.myservice" android:enabled="true" /> here myservice.java: (eclipse don't report error when try on device, apps give force close.)
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.os.ibinder; import android.widget.toast; public class myservice extends service implements sensoreventlistener { sensor proxsensor; sensormanager sm; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { toast.maketext(this, "service created", toast.length_long).show(); sm=(sensormanager)getsystemservice(sensor_service); proxsensor=sm.getdefaultsensor(sensor.type_proximity); sm.registerlistener(this, proxsensor, sensormanager.sensor_delay_normal); } @override public void onaccuracychanged(sensor sensor, int accuracy) { // todo auto-generated method stub } @override public void onsensorchanged(sensorevent event) { // todo auto-generated method stub intent panel = new intent(this, panel.class); startactivity(panel); } @override public void ondestroy() { toast.maketext(this, "service stopped", toast.length_long).show(); } @override public void onstart(intent intent, int startid) { toast.maketext(this, "service started", toast.length_long).show(); } } and here main.java :
import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import com.lino99.smarttask.r; public class main extends activity implements onclicklistener { button buttonstart, buttonstop; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); buttonstart = (button) findviewbyid(r.id.buttonstart); buttonstop = (button) findviewbyid(r.id.buttonstop); buttonstart.setonclicklistener(this); buttonstop.setonclicklistener(this); } public void onclick(view src) { switch (src.getid()) { case r.id.buttonstart: startservice(new intent(this, myservice.class)); break; case r.id.buttonstop: stopservice(new intent(this, myservice.class)); break; } } }
ok here few changes in code should clear errors , make service ongoing , proximity sensor responsive:
this how service should like:
@override public void oncreate() {//oncreat shouldn't used sensor u should use onstartcommand toast.maketext(this, "service created", toast.length_long).show(); } @override public void onaccuracychanged(sensor sensor, int accuracy) { // todo auto-generated method stub } @override public void onsensorchanged(sensorevent event) { // todo auto-generated method stub //here u should use following code otherwise sensor crazy if(event.values[0] == 0){ intent panel = new intent(this, panel.class); startactivity(panel); } } @override public void ondestroy() {//here u should unregister sensor toast.maketext(this, "service stopped", toast.length_long).show(); sm.unregisterlistener(this); } @override//here u should register sensor , write onstartcommand not onstart public int onstartcommand(intent intent, int flags, int startid) { toast.maketext(this, "service started", toast.length_long).show(); sm=(sensormanager)getsystemservice(sensor_service); proxsensor=sm.getdefaultsensor(sensor.type_proximity); sm.registerlistener(this, proxsensor, sensormanager.sensor_delay_normal); //here u should make service foreground keep working if app closed notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(notification_service); intent bintent = new intent(myservice.this, mainactivity.class); pendingintent pbintent = pendingintent.getactivity(myservice.this, 0 , bintent, 0); notificationcompat.builder bbuilder = new notificationcompat.builder(this) .setsmallicon(r.drawable.ic_launcher) .setcontenttitle("title") .setcontenttext("subtitle") .setautocancel(true) .setongoing(true) .setcontentintent(pbintent); barnotif = bbuilder.build(); this.startforeground(1, barnotif); //then should return sticky return service.start_sticky; } }
ok try code , let me know how works you, hope helped.
Comments
Post a Comment