i have service uses following code:
package com.ritvik.weaselyclock; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.intentservice; import android.app.notificationmanager; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.os.bundle; import android.os.handler; import android.support.v4.app.notificationcompat; import android.support.v4.app.taskstackbuilder; import android.util.log; import android.widget.toast; import com.google.android.gms.gcm.googlecloudmessaging; import com.ritvik.utils.jsonutils; public class gcmintentservice extends intentservice { public final static string clock_ang = "com.tutecentral.navigationdrawer.clockang"; public static final int notification_id = 1; notificationcompat.builder builder; handler mhandler; static final string tag = "receive message activity"; public gcmintentservice() { super("44937255078"); } @override public int onstartcommand(intent intent, int flags, int startid) { mhandler = new handler(); return startid; } @override protected void onhandleintent(intent intent) { bundle extras = intent.getextras(); googlecloudmessaging gcm = googlecloudmessaging.getinstance(this); // getmessagetype() intent parameter must intent received // in broadcastreceiver. string messagetype = gcm.getmessagetype(intent); if (!extras.isempty()) { // has effect of unparcelling bundle /* * filter messages based on message type. since gcm * extended in future new message types, ignore * message types you're not interested in, or don't * recognize. */ if (googlecloudmessaging. message_type_send_error.equals(messagetype)) { } else if (googlecloudmessaging. message_type_deleted.equals(messagetype)) { // if it's regular gcm message, work. } else if (googlecloudmessaging. message_type_message.equals(messagetype)) { // post notification of received message. log.i(tag, "received: " + extras.tostring()); handlemessage(extras.getstring("message")); } } } public void handlemessage(final string msg) { mhandler.post(new runnable() { @override public void run() { jsonobject json = null; string type = ""; try { json = new jsonobject(msg); type = json.getstring("type"); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } if (type.equals("ru_101")) { startactivity(new intent(gcmintentservice.this, mainactivity.class)); toast.maketext(gcmintentservice.this, "registration successful", toast.length_long).show(); } else if (type.equals("ru_002")) { toast.maketext(gcmintentservice.this, "registration updated", toast.length_long).show(); } else if (type.equals("ru_301")) { notificationcompat.builder mbuilder = new notificationcompat.builder(gcmintentservice.this) .setsmallicon(r.drawable.ic_launcher) .setcontenttitle("my notification") .setcontenttext("hello world!"); // creates explicit intent activity in app intent resultintent = new intent(gcmintentservice.this, resultactivity.class); try { resultintent.putextra("com.ritvik.weaselyclock.phone", json.getstring("phone_number")); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } // stack builder object contain artificial stack // started activity. // ensures navigating backward activity leads out of // application home screen. taskstackbuilder stackbuilder = taskstackbuilder.create(gcmintentservice.this); // adds stack intent (but not intent itself) stackbuilder.addparentstack(resultactivity.class); // adds intent starts activity top of stack stackbuilder.addnextintent(resultintent); pendingintent resultpendingintent = stackbuilder.getpendingintent( 0, pendingintent.flag_update_current ); mbuilder.addaction(r.drawable.ic_action_good, "accept", resultpendingintent); mbuilder.addaction(r.drawable.ic_action_discard, "reject", resultpendingintent); notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service); int mid = 001; // mid allows update notification later on. mnotificationmanager.notify(mid, mbuilder.build()); } else if ( type.equals("ru_101") || type.equals("ru_102") || type.equals("ru_103") || type.equals("ru_201") || type.equals("ru_202") || type.equals("ru_203") || type.equals("ru_204") || type.equals("ru_205") || type.equals("ru_206") || type.equals("ru_207") || type.equals("ru_208") || type.equals("ru_209") || type.equals("ru_210") || type.equals("ru_211") || type.equals("jr_201") || type.equals("jr_202") || type.equals("jr_203") || type.equals("jr_204") || type.equals("jr_205") || type.equals("jr_206") || type.equals("jr_207") || type.equals("jr_208") || type.equals("jr_209") || type.equals("id_201") || type.equals("id_202") || type.equals("id_203") || type.equals("id_204") || type.equals("id_205") || type.equals("id_206") || type.equals("id_207") || type.equals("id_208") || type.equals("sm_201") || type.equals("sm_202") || type.equals("sm_203") || type.equals("sm_204") || type.equals("sm_205") || type.equals("sm_206") || type.equals("sm_207") || type.equals("sm_208") || type.equals("sm_209") || type.equals("sm_210")) { try { log.e("server error:", json.getstring("error")); toast.maketext(gcmintentservice.this, "server error: " + json.getstring("error"), toast.length_long).show(); } catch (jsonexception e) { e.printstacktrace(); } } else if (type.equals("jr_101") || type.equals("jr_102")) { try { toast.maketext(gcmintentservice.this, json.getstring("action"), toast.length_long).show(); } catch (jsonexception e) { e.printstacktrace(); } } else if (type.equals("jr_301") || type.equals("jr_302")) { try { toast.maketext(gcmintentservice.this, "you have been " + json.getstring("action") + "by group" + json.getstring("group") , toast.length_long).show(); } catch (jsonexception e) { e.printstacktrace(); } } else if (type.equals("id_101")) { try { jsonarray members = json.getjsonarray("members"); jsonutils.writejson("com.ritvik.weaselyclock.members", gcmintentservice.this, members, "members"); } catch (jsonexception e) { e.printstacktrace(); } } } }); } }
for reason, according debugger, onstartcommand() method never being called. i'd tried use oncreate(), wasn't being called either. app crashes following logcat output:
04-28 08:25:35.699: e/androidruntime(11113): fatal exception: thread-775 04-28 08:25:35.699: e/androidruntime(11113): java.lang.nullpointerexception 04-28 08:25:35.699: e/androidruntime(11113): @ com.ritvik.weaselyclock.gcmintentservice.handlemessage(gcmintentservice.java:69) 04-28 08:25:35.699: e/androidruntime(11113): @ com.ritvik.utils.httputils$1.run(httputils.java:48) 04-28 08:25:35.699: e/androidruntime(11113): @ java.lang.thread.run(thread.java:838)
edit: issue isn't methods aren't being called handler not being initialized properly.
any appreciated. thanks.