android - Starting a service and auto login from IntentService -
i have intentservice below should start messagingservice.class , automatically login user app , send notification. @ present, upon receiving gcm message, notification sent, nothin else, not toast messages in part of intentservice appear.
why case?
intentservice:
public class gcmintentservice extends intentservice { public static final int notification_id = 1; private notificationmanager mnotificationmanager; notificationcompat.builder builder; // logging stuff: protected static final int not_connected_to_service = 0; protected static final int fill_both_username_and_password = 1; public static final string authentication_failed = "0"; public static final string friend_list = "friend_list"; protected static final int make_sure_username_and_password_correct = 2; protected static final int not_connected_to_network = 3; private manager imservice; public static final int sign_up_id = menu.first; public static final int exit_app_id = menu.first + 1; // gcm string regid; googlecloudmessaging gcm; atomicinteger msgid = new atomicinteger(); sharedpreferences prefs; context context; public static final string extra_message = "message"; public static final string property_reg_id = "registration_id"; private static final string property_app_version = "appversion"; private final static int play_services_resolution_request = 9000; // end logging in public gcmintentservice() { super("gcmintentservice"); } public static final string tag = "gcmnotificationintentservice"; @override protected void onhandleintent(intent intent) { // start service messages: intent beginmessageservice = new intent(getbasecontext(), messagingservice.class); startservice(beginmessageservice); // try log in: toast.maketext(getapplicationcontext(), "successfully relieved feast gcm", toast.length_long).show(); // , log in automatically: //if not logged in nad shared preferences exists logged in once: if (!savesharedpreference.getusername(getapplicationcontext()).isempty()) { //startservice(new intent(this, messagingservice.class)); final string username = savesharedpreference.getusername(getapplicationcontext()); final string password = savesharedpreference.getpassword(getapplicationcontext()); toast.maketext(getapplicationcontext(), "sharedpref username " + username + " , password " + password, toast.length_long).show(); toast.maketext(getapplicationcontext(), "the value of imservice is: " + getapplicationcontext(), toast.length_long).show(); if (imservice == null) { toast.maketext(getapplicationcontext(), r.string.not_connected_to_service, toast.length_long).show(); // showdialog(not_connected_to_service); //return; } else if (imservice.isnetworkconnected() == false) { toast.maketext(getapplicationcontext(), r.string.not_connected_to_network, toast.length_long).show(); // showdialog(not_connected_to_network); } else { toast.maketext(getapplicationcontext(), "sharedpref username " + username + " , password " + password, toast.length_long).show(); thread loginthread = new thread() { private handler handler = new handler(); @override public void run() { string result = null; try { result = imservice.authenticateuser( username.trim(), password.trim()); } catch (unsupportedencodingexception e) { e.printstacktrace(); } if (result == null || result.equals(authentication_failed)) { /* * authenticatin failed, inform user */ handler.post(new runnable() { public void run() { toast.maketext( getapplicationcontext(), r.string.make_sure_username_and_password_correct, toast.length_long).show(); // showdialog(make_sure_username_and_password_correct); } }); } else { /* * if result not equal authentication failed, * result equal friend , group list of * user 0: friends, 1: groups */ } } }; loginthread.start(); } //setresultcode(activity.result_ok); } bundle extras = intent.getextras(); googlecloudmessaging gcm = googlecloudmessaging.getinstance(this); string messagetype = gcm.getmessagetype(intent); if (!extras.isempty()) { if (googlecloudmessaging.message_type_send_error .equals(messagetype)) { sendnotification("send error: " + extras.tostring()); } else if (googlecloudmessaging.message_type_deleted .equals(messagetype)) { sendnotification("deleted messages on server: " + extras.tostring()); } else if (googlecloudmessaging.message_type_message .equals(messagetype)) { (int = 0; < 3; i++) { log.i(tag, "working... " + (i + 1) + "/5 @ " + systemclock.elapsedrealtime()); try { thread.sleep(5000); } catch (interruptedexception e) { } } log.i(tag, "completed work @ " + systemclock.elapsedrealtime()); sendnotification("message received google gcm server: " + extras.get(config.message_key)); log.i(tag, "received: " + extras.tostring()); // start background service: // auto log user in based on store preferences: } } gcmbroadcastreceiver.completewakefulintent(intent); } private void sendnotification(string msg) { log.d(tag, "preparing send notification...: " + msg); mnotificationmanager = (notificationmanager) .getsystemservice(context.notification_service); pendingintent contentintent = pendingintent.getactivity(this, 0, new intent(this, mainactivity.class), 0); notificationcompat.builder mbuilder = new notificationcompat.builder( this).setsmallicon(r.drawable.gcm_cloud) .setcontenttitle("gcm notification") .setstyle(new notificationcompat.bigtextstyle().bigtext(msg)) .setcontenttext(msg); mbuilder.setcontentintent(contentintent); mnotificationmanager.notify(notification_id, mbuilder.build()); log.d(tag, "notification sent successfully."); } }
Comments
Post a Comment