Android的 - 如何运行通过&QUOT任务;处理"定期内的服务,意图(工作线程) [英] Android - how to run a task via "handler" periodically within a service-intent (worker-thread)

查看:186
本文介绍了Android的 - 如何运行通过&QUOT任务;处理"定期内的服务,意图(工作线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是有关Android的:
我怎样在一个intentservice内运行任务每20秒?
问题是,我要初始化一些类将在处理程序运行过程中使用。
它的工作原理一次 - 但随后该服务停止和放大器;当处理回路20秒后再次启动应用程序崩溃(因为当服务停止时得到了淘汰类的可能?)。所以,也许解决方案是让服务保持运行,只要处理程序运行或扔掉code和做是正确的?

希望,有人能帮助我。

 公共类Fadenzieher扩展IntentService { 私人处理程序处理程序=新的处理程序(); 私人Runnable接口timedTask =新的Runnable(){      @覆盖
      公共无效的run(){    //我得到的功能这里所说的...
            // class1member.getDBWorkdone();       handler.postDelayed(timedTask,20000);
       handler.obtainMessage();      }};公共Fadenzieher(){
    超级(Fadenzieher);}@覆盖
  保护无效onHandleIntent(意向意图){    //初始化一些
    //我要初始化一些增值分销商和放大器;这里说的功能
            //也将在处理循环内部使用
            // Class1的class1member =新1级();
    // class1member.startUpDB();          handler.post(timedTask); }

非常感谢你提前!

----所以这是更新code现在(14 2011年11月)

 公共类Fadenzieher延伸服务{
  私有静态最后的更新周期长= 60000;
上下文的背景下=这;
私人定时器定时器=新的Timer();
DbHelper dbHelper;公共无效的onCreate(){
dbHelper =新DbHelper(背景);
runTheLoop();
} 保护无效runTheLoop(){    timer.scheduleAtFixedRate(新的TimerTask(){
        @覆盖
        公共无效的run(){        dbHelper.dosomethings();
        Toast.makeText(上下文,CALL,Toast.LENGTH_LONG).show();
        }},0,更新周期);  }@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
    Toast.makeText(这一点,Starte服务,Toast.LENGTH_SHORT).show();   返回super.onStartCommand(意向,旗帜,startId);
}公共无效的onDestroy(){
    super.onDestroy();
    dbHelper.close();
    Toast.makeText(这一点,Stoppe服务,Toast.LENGTH_LONG).show();}//我们在bindService的调用返回的粘合剂类
@覆盖
公众的IBinder onBind(意向为arg0){
    返回mBinder;
}公共类MyBinder扩展粘结剂{
    Fadenzieher的getService(){
        返回Fadenzieher.this;
    }
}

}

整个应用程序立即崩溃。


解决方案

  

我如何运行的任务每20秒内intentservice?


这是不恰当的使用 IntentService 的。使用普通的服务,请


  

它的工作原理一次 - 但随后该服务停止和放大器;当处理回路20秒后再次启动该应用程序崩溃。


IntentService 关机时 onHandleIntent()的回报,这就是为什么这是打破了你。使用普通的服务,请

还有:


  • 请允许用户配置轮询周期

  • 确保这项服务将关机,当用户不再希望它运行

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.

解决方案

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

这篇关于Android的 - 如何运行通过&QUOT任务;处理"定期内的服务,意图(工作线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆