Android - how to run a task via “handler” periodically within a service-intent (worker-thread)

十年热恋 提交于 2019-12-11 18:17:05

问题


My question is Android related: How do I run a task every 20 seconds within an intentservice ? Problem is, I have to init some classes which will be used in the Handler "run" process. It works one time - but then the service stops & the application crashes when the handler-loop starts again after 20 seconds (probably because of the classes that got eliminated when the service stopped?). So maybe the solution is to get the service to stay running as long as the Handler runs or to throw away the code and do it right ?

Hope, someone can help me.

public class Fadenzieher extends IntentService{

 private Handler handler = new Handler();

 private Runnable timedTask = new Runnable(){

      @Override
      public void run() {

    // My functions get called here... 
            // class1member.getDBWorkdone(); 

       handler.postDelayed(timedTask, 20000); 
       handler.obtainMessage();

      }};

public Fadenzieher() {
    super("Fadenzieher");

}

@Override
  protected void onHandleIntent(Intent intent) {

    // SOME INITIALISING
    // I have to init some vars & functions here that 
            // will also be used inside the handler loop
            // Class1 class1member = new Class1();
    // class1member.startUpDB();

          handler.post(timedTask); }

Thank you very much in advance!!!

---- So this is the updated code now (14. nov. 2011)

public class Fadenzieher extends Service{
  private static final long UPDATE_INTERVAL = 60000;
Context context = this;
private Timer timer = new Timer();
DbHelper dbHelper;

public void onCreate(){
dbHelper = new DbHelper(context);
runTheLoop();
}

 protected void runTheLoop() {

    timer.scheduleAtFixedRate(new TimerTask(){
        @Override
        public void run() {

        dbHelper.dosomethings();
        Toast.makeText(context, "CALL", Toast.LENGTH_LONG).show();
        }}, 0, UPDATE_INTERVAL);

  }

@Override   
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "Starte Service“, Toast.LENGTH_SHORT).show();

   return super.onStartCommand(intent,flags,startId);
}

public void onDestroy() {
    super.onDestroy();
    dbHelper.close();
    Toast.makeText(this, "Stoppe Service“, Toast.LENGTH_LONG).show();

}

// We return the binder class upon a call of bindService
@Override
public IBinder onBind(Intent arg0) {
    return mBinder;
}

public class MyBinder extends Binder {
    Fadenzieher getService() {
        return Fadenzieher.this;
    }
}

}

The whole application crashes immediately.


回答1:


How do I run a task every 20 seconds within an intentservice ?

That is not an appropriate use of IntentService. Use a regular Service, please.

It works one time - but then the service stops & the application crashes when the handler-loop starts again after 20 seconds

IntentService shuts down when onHandleIntent() returns, which is why this is breaking for you. Use a regular Service, please.

Also:

  • Please allow the user to configure the polling period
  • Make sure that this service will shut down when the user no longer wants it to be running


来源:https://stackoverflow.com/questions/8101582/android-how-to-run-a-task-via-handler-periodically-within-a-service-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!