how can start and stop handler service in android -
i develop app use services. want start , stop service when phone in sleep mode. using following code not work.
my status checker services code
public void onstart(intent intent, int startd) { log.v("check","services running"); log.v("spinner value","services running"); handler.removecallbacks(sendnetworkstatus); handler.postdelayed(sendnetworkstatus, 1000); datamanager = (connectivitymanager)getsystemservice(connectivity_service); wifimanager = (wifimanager) getsystemservice(context.wifi_service); boolean screenon = intent.getbooleanextra("screen_state", false); if (!screenon) { // code log.v("screen","screen on>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); /* if(!ismyservicerunning()) { startservice(intent); }*/ } else { // code // stopservice(intent); log.v("screen","screen off>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); } } @override public ibinder onbind(intent intent) { // todo auto-generated method stub return null; } private runnable sendnetworkstatus = new runnable() { public void run() { sendnetworkinfo(); handler.postdelayed(this, 5000); checkstrength(wifistatus,mobilestatus); messageshow(response); log.v("check","ping url again , again"); } }; /*@override public boolean onunbind(intent intent) { handler.removecallbacks(sendnetworkstatus); handler = null; return super.onunbind(intent); }*/ private void sendnetworkinfo() { connectivitymanager connection = (connectivitymanager) this.getsystemservice(context.connectivity_service); networkinfo activenetwork = connection.getactivenetworkinfo(); if (null != activenetwork) { if(activenetwork.gettype() == connectivitymanager.type_wifi) {//wifistatus = "wifi status: up"; wifistatus= getwifisignalstrengthindbm(); telephonymanager.listen(phonestatelistener, phonestatelistener.listen_signal_strengths); } else{ wifistatus=0; } if(activenetwork.gettype() == connectivitymanager.type_mobile) {//mobilestatus = "mobile status: up"; message="mobile"; // filter.addaction("mobile"); telephonymanager.listen(phonestatelistener, phonestatelistener.listen_signal_strengths); } else{ //mobilestatus=0; telephonymanager.listen(phonestatelistener, phonestatelistener.listen_signal_strengths); } try { response = network.ping(url); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } else{ mobilestatus = 0; wifistatus = 0; response="-1"; } intent.putextra("status", response); sendbroadcast(intent); } public int getwifisignalstrengthindbm() { wifiinfo wifiinfo = wifimanager.getconnectioninfo(); return wifiinfo.getrssi(); } public void checkstrength(int wifi,int mobile) { if(wifi!=0 && mobile!=0) { if(wifi<mobile){ response="-2"; wifimanager.setwifienabled(false); internetcontrol.enableinternet(getapplicationcontext()); } } else if(wifi==0&&mobile<=-111) { response="-3"; message="signal strength weak"; } } @override public void ondestroy() { handler.removecallbacks(sendnetworkstatus); thread.currentthread().interrupt(); unregisterreceiver(mreceiver); toast.maketext(getbasecontext(), "service destroy", toast.length_long).show(); super.ondestroy(); }
and log cat error
09-04 12:46:33.249: e/androidruntime(4177): fatal exception: main 09-04 12:46:33.249: e/androidruntime(4177): java.lang.runtimeexception: unable start service com.example.data_connection_monitor.statuschecker@415923f8 null: java.lang.nullpointerexception 09-04 12:46:33.249: e/androidruntime(4177): @ android.app.activitythread.handleserviceargs(activitythread.java:2390) 09-04 12:46:33.249: e/androidruntime(4177): @ android.app.activitythread.access$1900(activitythread.java:128) 09-04 12:46:33.249: e/androidruntime(4177): @ android.app.activitythread$h.handlemessage(activitythread.java:1224) 09-04 12:46:33.249: e/androidruntime(4177): @ android.os.handler.dispatchmessage(handler.java:99) 09-04 12:46:33.249: e/androidruntime(4177): @ android.os.looper.loop(looper.java:137) 09-04 12:46:33.249: e/androidruntime(4177): @ android.app.activitythread.main(activitythread.java:4517) 09-04 12:46:33.249: e/androidruntime(4177): @ java.lang.reflect.method.invokenative(native method) 09-04 12:46:33.249: e/androidruntime(4177): @ java.lang.reflect.method.invoke(method.java:511) 09-04 12:46:33.249: e/androidruntime(4177): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:993) 09-04 12:46:33.249: e/androidruntime(4177): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:760) 09-04 12:46:33.249: e/androidruntime(4177): @ dalvik.system.nativestart.main(native method) 09-04 12:46:33.249: e/androidruntime(4177): caused by: java.lang.nullpointerexception 09-04 12:46:33.249: e/androidruntime(4177): @ com.example.data_connection_monitor.statuschecker.onstart(statuschecker.java:77) 09-04 12:46:33.249: e/androidruntime(4177): @ android.app.service.onstartcommand(service.java:438) 09-04 12:46:33.249: e/androidruntime(4177): @ android.app.activitythread.handleserviceargs(activitythread.java:2373) 09-04 12:46:33.249: e/androidruntime(4177): ... 10 more
please me. new in android. in advance!!!!!!!!
Comments
Post a Comment