android - Cannot Run Tasks in Service -


i know basic question, new android service. have done research on google , stackoverflow. there many question in stackoverflow related or similar topic, couldn't able proper answer , being diverted different topics.

this simple test code running.

public class service extends android.app.service {  private handler mhandler;  private void ping() {     try {         log.e("tag", "success");         toast.maketext(getapplicationcontext(), "service ping", toast.length_short).show();      } catch (exception e) {         log.e("error", "in onstartcommand");         e.printstacktrace();     }     schedulenext(); }  private void schedulenext() {     mhandler.postdelayed(new runnable() {         public void run() { ping(); }     }, 3000); }  public int onstartcommand(intent intent, int x, int y) {     mhandler = new android.os.handler();     ping();     return start_sticky; }  @override public ibinder onbind(intent intent) {     return null; }  } 

in toast message pops , log message printed every 3 seconds, works when app minimized. when quit app, there no toast or log printed. in so answer in says why toast message cannot called without ui. , cannot print log process being killed.

basically, want service run in background every 5 min , need data online. how should implement service? , example code or tutorials appreciated?

when start service, default runs in same process whatever component started it. when process component running in quits, service. in order start service in own process, need following in manifest:

    <service         android:name=".service"         android:enabled="true"         android:exported="false"         android:process=":separate_service_process"         android:stopwithtask="false" >     </service> 

putting colon in front of label android:process attribute tells system start service in separate process, , android:stopwithtask attribute tell system keep service alive when component started stops. see http://developer.android.com/guide/topics/manifest/service-element.html more information on manifest settings (the stopwithtask attribute part of serviceinfo class).

now start service using startservice(intent) , should set. luck!

ps--i'd recommend renaming service class unique avoid confusion base service class.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -