java - Android Service unexpectedly stoppping -
for android application i'm making, want there service checking database attack when app isnt running (kinda how facebook notify if it's closed) , made class below, yet minute after stop app running message "app has unexpetedly stopped" , again few minutes later...i know possible keep checking somehow cause many apps doing wrong? have far:
package com.ducknoise.toonfight; import java.sql.sqlexception; import android.app.notificationmanager; import android.app.service; import android.content.intent; import android.os.asynctask; import android.os.build; import android.os.ibinder; import android.support.v4.app.notificationcompat; import android.support.v4.app.notificationcompat.builder; import android.widget.toast; public class toonservice extends service{ public static toonservice toonservice; public toonservice(){ toonservice = this; } @override public ibinder onbind(intent intent) { string toonname = intent.getstringextra("toonname"); toondb task = new toondb(); if (build.version.sdk_int >= build.version_codes.honeycomb) { task.executeonexecutor(asynctask.thread_pool_executor,toonname); } else { task.execute(toonname); } return null; } @override public int onstartcommand(intent intent, int flags, int startid) { string toonname = intent.getstringextra("toonname"); toondb task = new toondb(); if (build.version.sdk_int >= build.version_codes.honeycomb) { task.executeonexecutor(asynctask.thread_pool_executor,toonname); } else { task.execute(toonname); } return service.start_sticky; } @override public void oncreate() { super.oncreate(); } @override public void ondestroy() { toast.maketext(getapplicationcontext(), "disconnected server", toast.length_long).show(); database db = new database(); db.closeconnection(); super.ondestroy(); } public class toondb extends asynctask<string,void, void>{ toon toon; @override protected void doinbackground(string... params) { database db = new database(); db.establishconnection(); toon = db.gettoonfromdb(params[0]); return null; } @override protected void onpostexecute(void result) { tooncheck task = new tooncheck(); if (build.version.sdk_int >= build.version_codes.honeycomb) { task.executeonexecutor(asynctask.thread_pool_executor,toon); } else { task.execute(toon); } super.onpostexecute(result); } } public class tooncheck extends asynctask<toon,void, void>{ boolean runcheck; toon toon; @override protected void doinbackground(toon... params) { runcheck = true; database db = new database(); db.establishconnection(); toon = params[0]; toon.updatecheckdb(); publishprogress(); if(!db.isonline(toon)) runcheck = false; return null; } @override protected void onprogressupdate(void... values) { battle battle = toon.getbattle(); if(battle != null ){ if(battle.notified()){ notificationcompat.builder nbuilder = new notificationcompat.builder(getapplicationcontext()); nbuilder.setcontenttitle("toon fight"); string contenttext = battle.getoppname() + " has attacked you!"; nbuilder.setcontenttext(contenttext); nbuilder.setsmallicon(r.drawable.ic_launcher); int notiid = 1295; notificationmanager notificationmanager = (notificationmanager)getsystemservice(notification_service); notificationmanager.notify(notiid, nbuilder.build()); } } super.onprogressupdate(values); } @override protected void onpostexecute(void result) { if(runcheck){ tooncheck task = new tooncheck(); if (build.version.sdk_int >= build.version_codes.honeycomb) { task.executeonexecutor(asynctask.thread_pool_executor,toon); } else { task.execute(toon); } }else stopself(); super.onpostexecute(result); } } }
this logcat
03-24 11:01:41.968: d/androidruntime(4624): shutting down vm 03-24 11:01:41.968: w/dalvikvm(4624): threadid=1: thread exiting uncaught exception (group=0x41f5aac8) 03-24 11:01:41.968: e/androidruntime(4624): fatal exception: main 03-24 11:01:41.968: e/androidruntime(4624): java.lang.runtimeexception: unable start service com.ducknoise.toonfight.toonservice@430a3950 null: java.lang.nullpointerexception 03-24 11:01:41.968: e/androidruntime(4624): @ android.app.activitythread.handleserviceargs(activitythread.java:2782) 03-24 11:01:41.968: e/androidruntime(4624): @ android.app.activitythread.access$2000(activitythread.java:152) 03-24 11:01:41.968: e/androidruntime(4624): @ android.app.activitythread$h.handlemessage(activitythread.java:1385) 03-24 11:01:41.968: e/androidruntime(4624): @ android.os.handler.dispatchmessage(handler.java:99) 03-24 11:01:41.968: e/androidruntime(4624): @ android.os.looper.loop(looper.java:137) 03-24 11:01:41.968: e/androidruntime(4624): @ android.app.activitythread.main(activitythread.java:5329) 03-24 11:01:41.968: e/androidruntime(4624): @ java.lang.reflect.method.invokenative(native method) 03-24 11:01:41.968: e/androidruntime(4624): @ java.lang.reflect.method.invoke(method.java:511) 03-24 11:01:41.968: e/androidruntime(4624): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) 03-24 11:01:41.968: e/androidruntime(4624): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) 03-24 11:01:41.968: e/androidruntime(4624): @ dalvik.system.nativestart.main(native method) 03-24 11:01:41.968: e/androidruntime(4624): caused by: java.lang.nullpointerexception 03-24 11:01:41.968: e/androidruntime(4624): @ com.ducknoise.toonfight.toonservice.onstartcommand(toonservice.java:34) 03-24 11:01:41.968: e/androidruntime(4624): @ android.app.activitythread.handleserviceargs(activitythread.java:2765) 03-24 11:01:41.968: e/androidruntime(4624): ... 10 more
this how i'm calling service:
intent toonservice = new intent(mainactivity.this, toonservice.class); toonservice.putextra("toonname", toon.getname()); mainactivity.this.startservice(toonservice);
you should check if intent
@ onstartcommand
null
or not per logcat
:
03-24 11:01:41.968: e/androidruntime(4624): caused by: java.lang.nullpointerexception 03-24 11:01:41.968: e/androidruntime(4624): @ com.ducknoise.toonfight.toonservice.onstartcommand(toonservice.java:34)
and please show how start service
Comments
Post a Comment