performance - How can i start my service in background on boot in android -
this question has answer here:
- android -starting service @ boot time 7 answers
here service class how can run everytime
public class syncservice extends service { private string loginuserinfoid = ""; sessionmanager session; databasehelper db; messagelistactivity mla; string otherfilename = ""; int timer = 3000; long totalsize = 0; @override public void oncreate() { super.oncreate(); } @override public void ondestroy() { super.ondestroy(); } @override public int onstartcommand(intent intent, int flags, int startid) { db = new databasehelper(getapplicationcontext()); mla = new messagelistactivity(); session = new sessionmanager(getapplicationcontext()); session.checklogin(); hashmap<string, string> user = session.getuserdetails(); loginuserinfoid = user.get(sessionmanager.key_user_info_id); final handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { syncmessagefromserver(); } }, timer); final handler handlert = new handler(); handlert.postdelayed(new runnable() { @override public void run() { syncpendingmessagetoserver(); } }, 5000); return super.onstartcommand(intent, flags, startid); } @override public ibinder onbind(intent arg0) { return null; } private void syncmessagefromserver() { if(config.isinterneton(getapplicationcontext()) && loginuserinfoid != "") { string str = ""; httpresponse response; httpclient myclient = new defaulthttpclient(); httppost myconnection = new httppost(config.host_name+"/androidapp/getallmessage/" + loginuserinfoid); try { response = myclient.execute(myconnection); str = entityutils.tostring(response.getentity(), "utf-8"); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { jsonarray jarray = new jsonarray(str); (int = 0; <= jarray.length() - 1; i++) { jsonobject row = jarray.getjsonobject(i); if (row.getstring("senderuserid") != loginuserinfoid) { chatmessage cm = new chatmessage(); string currentdatetime = config.getcurrentdatetime(); string filetype = ""; string upfileurl = row.getstring("fileattachedurl").replace(" ", "%20"); if (upfileurl.contains(".jpg") || upfileurl.contains(".png")) { filetype = "image"; } else if (upfileurl.contains(".mp4")) { filetype = "video"; } else if (upfileurl.contains(".mp3")) { filetype = "audio"; } else { filetype = ""; } /******* firstly take data in model object ******/ cm.setoriginalmsgthreadid(row.getstring("messagethreadid")); cm.setsenderuserinfoid(row.getstring("senderuserid")); cm.setreceiveruserinfoid(row.getstring("multiplereceiversid")); cm.setmessagestatus("sent"); cm.setispending(0); cm.setispendingtoupdate(1); cm.setmessagetext(row.getstring("messagetext")); cm.setmediaurl(""); cm.setonlinemediaurl(config.host_name + row.getstring("fileattachedurl").replace(" ", "%20")); cm.setthumbimage(""); cm.setisdownloaded(0); cm.setmediamimetype(filetype); cm.setmediasize(0); cm.setmedianame(""); cm.setlatitude(""); cm.setlongitude(""); cm.setsendtimestamp(currentdatetime); cm.setreceivetimestamp(""); long messageid = db.sendmessage(cm); //log.d("myservice", "chatmessageid = " + messageid); intent imla = new intent("refresh_data"); imla.putextra("id", messageid); //intent.putextra("id", messageid); sendbroadcast(imla); string notiavatarurl = ""; string notiusername = ""; list<chatmessagethread> allchatmessagethread = db.getmessagethreadsbyid(row.getstring("senderuserid")); (chatmessagethread chatmessagethread : allchatmessagethread) { notiavatarurl = chatmessagethread.getmessagethreadavatarurl(); notiusername = chatmessagethread.getmessagethreadtitle(); } if (row.getstring("senderuserid") != loginuserinfoid) { displaynotificationmessage(row.getstring("messagetext"), notiavatarurl, notiusername); } confirmsyncedtoserver(row.getstring("messageid")); } } } catch (jsonexception e) { e.printstacktrace(); } } final handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { syncmessagefromserver(); //syncpendingmessagetoserver(); } }, timer); } private void syncpendingmessagetoserver() { //toast.maketext(getapplicationcontext(), "toserver run", toast.length_long).show(); if(config.isinterneton(getapplicationcontext()) && loginuserinfoid != "") { list<chatmessage> allchatmessage = db.getallmessage(); (chatmessage chatmessage : allchatmessage) { if (chatmessage.getispending() == 1 && chatmessage.getmediamimetype().isempty()) { string msgtext = chatmessage.getmessagetext(); httpclient myclient = new defaulthttpclient(); httppost myconnection = new httppost(config.host_name+"/androidapp/sendmessage"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("messagetext", msgtext)); namevaluepairs.add(new basicnamevaluepair("senderuserinfoid", chatmessage.getsenderuserinfoid())); namevaluepairs.add(new basicnamevaluepair("recieveruserinfoid", chatmessage.getreceiveruserinfoid())); namevaluepairs.add(new basicnamevaluepair("url", "")); namevaluepairs.add(new basicnamevaluepair("isgroupmsg", "false")); myconnection.setentity(new urlencodedformentity(namevaluepairs)); myclient.execute(myconnection); } catch (clientprotocolexception e) { toast.maketext(getapplicationcontext(), "message sending failed!", toast.length_long).show(); //e.printstacktrace(); } catch (ioexception e) { //e.printstacktrace(); } } } db.updatesyncedmessage(); } final handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { syncpendingmessagetoserver(); } }, 10000); } public void confirmsyncedtoserver(string messageid){ httpresponse response; httpclient myclient = new defaulthttpclient(); httppost myconnection = new httppost(config.host_name+"/androidapp/messagesynced/" + messageid); try { response = myclient.execute(myconnection); } catch (clientprotocolexception e) { toast.maketext(getapplicationcontext(),"message sending failed!", toast.length_long).show(); //e.printstacktrace(); } catch (ioexception e) { //e.printstacktrace(); } } private void displaynotificationmessage(string message, string notiavatarurl, string notiusername) { notificationcompat.builder mbuilder = new notificationcompat.builder(this); bitmap bitmap = bitmapfactory.decodefile(notiavatarurl); mbuilder.setsmallicon(r.drawable.psklogo); mbuilder.setlargeicon(bitmap); mbuilder.setcontenttitle(notiusername); mbuilder.setcontenttext(message); mbuilder.setvibrate(new long[] { 500, 500}); mbuilder.setlights(color.red, 3000, 3000); try { uri notification = ringtonemanager.getdefaulturi(ringtonemanager.type_notification); mbuilder.setsound(notification); } catch (exception e) { e.printstacktrace(); } intent resultintent = new intent(this, messagethreadactivity.class); taskstackbuilder stackbuilder = taskstackbuilder.create(this); stackbuilder.addparentstack(messagethreadactivity.class); // adds intent starts activity top of stack stackbuilder.addnextintent(resultintent); pendingintent resultpendingintent = stackbuilder.getpendingintent( 0, pendingintent.flag_update_current ); mbuilder.setcontentintent(resultpendingintent); notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service); // notificationid allows update notification later on. mnotificationmanager.notify(100, mbuilder.build()); } }
create broadcastreceiver , register receive action_boot_completed. need receive_boot_completed permission.
read: listening , broadcasting global messages, , setting alarms
refer through answer
Comments
Post a Comment