android - Allowing service time to start up -


i wish enable automatic login app, yet problem arising service being started , bound after automatic login features being called:

04-28 20:21:09.061    5427-5427/com.example.feastapp i/personamanager﹕ getpersonaservice() name persona_policy 04-28 20:21:09.081    5427-5427/com.example.feastapp v/auto login test﹕ waiting... 1/5 @ 268919454 04-28 20:21:14.086    5427-5427/com.example.feastapp v/auto login test﹕ waiting... 2/5 @ 268924454 04-28 20:21:19.081    5427-5427/com.example.feastapp v/auto login test﹕ waiting... 3/5 @ 268929454 04-28 20:21:24.096    5427-5427/com.example.feastapp i/personamanager﹕ getpersonaservice() name persona_policy 04-28 20:21:24.176    5427-5427/com.example.feastapp v/test gcm service login﹕ service null 04-28 20:21:24.236    5427-5427/com.example.feastapp d/dalvikvm﹕ gc_for_alloc freed 244k, 17% free 6485k/7812k, paused 15ms, total 15ms 04-28 20:21:24.296    5427-5427/com.example.feastapp d/dalvikvm﹕ gc_for_alloc freed 1069k, 26% free 6881k/9260k, paused 9ms, total 10ms 04-28 20:21:24.376    5427-5427/com.example.feastapp d/libegl﹕ loaded /system/lib/egl/libegl_mali.so 04-28 20:21:24.381    5427-5427/com.example.feastapp d/libegl﹕ loaded /system/lib/egl/libglesv1_cm_mali.so 04-28 20:21:24.386    5427-5427/com.example.feastapp d/libegl﹕ loaded /system/lib/egl/libglesv2_mali.so 04-28 20:21:24.391    5427-5427/com.example.feastapp e/﹕ device driver api match     device driver api version: 23     user space api version: 23 04-28 20:21:24.391    5427-5427/com.example.feastapp e/﹕ mali: revision=linux-r3p2-01rel3 build_date=mon feb 10 15:44:10 kst 2014 04-28 20:21:24.446    5427-5427/com.example.feastapp d/openglrenderer﹕ enabling debug mode 0 04-28 20:21:24.506    5427-5427/com.example.feastapp v/service﹕ service connected 

i have service startup in extended application class give service time, yet still starts after auto log feature clause called.

the loggingin class:

public class loggingin extends activity {      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 edittext usernametext;     private edittext passwordtext;      private jsonobject resultobject;     public static string userid = null;      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;      private serviceconnection mconnection = new serviceconnection() {         public void onserviceconnected(componentname classname, ibinder service) {             imservice = ((messagingservice.imbinder) service).getservice();             log.v("service", "the service connected");               if (imservice.isuserauthenticated() == true) {                 // intent = new intent(loggingin.this, listoffriends.class);                 intent = new intent(loggingin.this, mainactivity.class);                 startactivity(i);                 loggingin.this.finish();             }         }          public void onservicedisconnected(componentname classname) {             log.v("service", "the service disconnected");             imservice = null;             toast.maketext(loggingin.this, r.string.local_service_stopped,                     toast.length_short).show();         }     };      /**      * called when activity first created.      */     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);               /*                * start , bind imservice                */         bindservice(new intent(loggingin.this, messagingservice.class),                 mconnection, context.bind_auto_create);           // initiate volley:         final requestqueue requestqueue = volleysingleton.getsinstance().getrequestqueue();          //remove title bar         this.requestwindowfeature(window.feature_no_title);          //remove notification bar         this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen);          // auto logging in:         if (!savesharedpreference.getusername(getapplicationcontext()).isempty()) {              (int = 0; < 3; i++) {                 log.v("auto login test",                         "waiting... " + (i + 1) + "/5 @ "                                 + systemclock.elapsedrealtime());                 try {                     thread.sleep(5000);                 } catch (interruptedexception e) {                 }              }              setcontentview(r.layout.feast_loading_splash_page);              final string username = savesharedpreference.getusername(getapplicationcontext());              final string password = savesharedpreference.getpassword(getapplicationcontext());              log.v("test gcm service login", "sharedpref username " + username + " , password " + password);              if (imservice == null) {                 log.v("test gcm service login", "service 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) {                 log.v("test gcm service login", "imservice.isnetworkconnected() == false");                 toast.maketext(getapplicationcontext(),                         r.string.not_connected_to_network,                         toast.length_long).show();                 // showdialog(not_connected_to_network);              } else {                  log.v("test gcm service login", "when imservice not null: sharedpref username " + username + " , password " + password);                  thread autologinthread = 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                                        */                              handler.post(new runnable() {                                 public void run() {                                     intent = new intent(loggingin.this,                                             mainactivity.class);                                     startactivity(i);                                     loggingin.this.finish();                                  }                             });                          }                      }                 };                 autologinthread.start();              }          }          setcontentview(r.layout.loggin_in);         setcontentview(r.layout.feast_login_page);         //settitle("login");          imagebutton loginbutton = (imagebutton) findviewbyid(r.id.button1);          // don't need log in manually, click button         usernametext = (edittext) findviewbyid(r.id.username);         passwordtext = (edittext) findviewbyid(r.id.password);          loginbutton.setonclicklistener(new onclicklistener() {             public void onclick(view arg0) {                 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 if (usernametext.length() > 0                         && passwordtext.length() > 0) {                      thread loginthread = new thread() {                         private handler handler = new handler();                          @override                         public void run() {                             string result = null;                              try {                                 result = imservice.authenticateuser(                                         usernametext.gettext().tostring().trim(),                                         passwordtext.gettext().tostring().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                                        */                                 handler.post(new runnable() {                                     public void run() {                                          // if log in successful, save                                         // username , password shared                                         // preferences:                                          savesharedpreference.setusername(                                                 getapplicationcontext(),                                                 usernametext.gettext()                                                         .tostring());                                          savesharedpreference.setpassword(                                                 getapplicationcontext(),                                                 passwordtext.gettext()                                                         .tostring());                                          intent = new intent(loggingin.this,                                                 mainactivity.class);                                         startactivity(i);                                         loggingin.this.finish();                                      }                                 });                              }                          }                     };                     loginthread.start();                  } else {                           /*                            * username or password not filled, alert user                            */                     toast.maketext(getapplicationcontext(),                             r.string.fill_both_username_and_password,                             toast.length_long).show();                     // showdialog(fill_both_username_and_password);                 }                  // users id!!                 jsonobjectrequest getuserid = new jsonobjectrequest(request.method.get,                         "http://" + global.getfeastonline() + "/getuserdata/" + usernametext.gettext().tostring() + ".json", ((string) null),                         new response.listener<jsonobject>() {                             @override                             public void onresponse(jsonobject response) {                                  // parse json:                                 try {                                     resultobject = response.getjsonobject("userinfo");                                     userid = resultobject.getstring("id");                                      log.v("user id", "the user id " + userid);                                  } catch (jsonexception e) {                                     e.printstacktrace();                                 }                              }                         },                         new response.errorlistener() {                             @override                             public void onerrorresponse(volleyerror error) {                                 log.d("error.response", error.tostring());                             }                         });                  requestqueue.add(getuserid);              }         });     }      @override     protected dialog oncreatedialog(int id) {         int message = -1;         switch (id) {             case not_connected_to_service:                 message = r.string.not_connected_to_service;                 break;             case fill_both_username_and_password:                 message = r.string.fill_both_username_and_password;                 break;             case make_sure_username_and_password_correct:                 message = r.string.make_sure_username_and_password_correct;                 break;             case not_connected_to_network:                 message = r.string.not_connected_to_network;                 break;             default:                 break;         }          if (message == -1) {             return null;         } else {             return new alertdialog.builder(loggingin.this)                     .setmessage(message)                     .setpositivebutton(r.string.ok,                             new dialoginterface.onclicklistener() {                                 public void onclick(dialoginterface dialog,                                                     int whichbutton) {                                                     /* user clicked ok stuff */                                 }                             }).create();         }     }      @override     protected void onpause() {         super.onpause();         unbindservice(mconnection);     }      @override     protected void onresume() {         super.onresume();          bindservice(new intent(loggingin.this, messagingservice.class),                 mconnection, context.bind_auto_create);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         boolean result = super.oncreateoptionsmenu(menu);          menu.add(0, sign_up_id, 0, r.string.sign_up);         menu.add(0, exit_app_id, 0, r.string.exit_application);          return result;     }      @override     public boolean onmenuitemselected(int featureid, menuitem item) {          switch (item.getitemid()) {             case sign_up_id:                 intent = new intent(loggingin.this, signingup.class);                 startactivity(i);                 return true;             case exit_app_id:                  return true;         }          return super.onmenuitemselected(featureid, item);     }  } 

i posted auto login code within serviceconnection block. issue service connected after oncreate block, therefore null when calling against it.

private serviceconnection mconnection = new serviceconnection() {         public void onserviceconnected(componentname classname, ibinder service) {             imservice = ((messagingservice.imbinder) service).getservice();             log.v("service", "the service connected");              // allowing autologin!!**********************************************************             final string username = savesharedpreference.getusername(getapplicationcontext());              final string password = savesharedpreference.getpassword(getapplicationcontext());              log.v("test gcm service login", "sharedpref username " + username + " , password " + password);              //log.v("test gcm service login", "the value of imservice is: " + imservice.tostring());              if (imservice == null) {                 log.v("test gcm service login", "service 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) {                 log.v("test gcm service login", "imservice.isnetworkconnected() == false");                 toast.maketext(getapplicationcontext(),                         r.string.not_connected_to_network,                         toast.length_long).show();                 // showdialog(not_connected_to_network);              } else {                  log.v("test gcm service login", "when imservice not null: sharedpref username " + username + " , password " + password);                  thread autologinthread = 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                                        */                              handler.post(new runnable() {                                 public void run() {                                     intent = new intent(loggingin.this,                                             mainactivity.class);                                     startactivity(i);                                     loggingin.this.finish();                                  }                             });                          }                      }                 };                 autologinthread.start();              }              if (imservice.isuserauthenticated() == true) {                 // intent = new intent(loggingin.this, listoffriends.class);                 intent = new intent(loggingin.this, mainactivity.class);                 startactivity(i);                 loggingin.this.finish();             }         }          public void onservicedisconnected(componentname classname) {              log.v("service", "the service disconnected");             imservice = null;             toast.maketext(loggingin.this, r.string.local_service_stopped,                     toast.length_short).show();         }     }; 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -