首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在后台服务中,在给定间隔后运行任务

在后台服务中,在给定间隔后运行任务
EN

Stack Overflow用户
提问于 2015-01-09 20:10:24
回答 1查看 43关注 0票数 0

我有一个我创建的后台任务

代码语言:javascript
复制
package com.cnn.service;

/**
 *
 * @author USER
 */
import android.app.Service;
import android.os.IBinder;
import android.content.Intent;

public class AndroidStartServiceOnBoot extends Service {

      @Override
      public IBinder onBind(Intent intent) {
            return null;
      }

      @Override
      public void onCreate() {
            super.onCreate();
                // do something when the service is created
      }
}

和另一个文件

代码语言:javascript
复制
package com.cnn.service;

/**
 *
 * @author USER
 */
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;

// here is the OnRevieve methode which will be called when boot completed
public class BootCompleted extends BroadcastReceiver{
     @Override
     public void onReceive(Context context, Intent intent) {
 //we double check here for only boot complete event
 if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
   {
     //here we start the service             
     Intent serviceIntent = new Intent(context, AndroidStartServiceOnBoot.class);
     context.startService(serviceIntent);
   }
 }
}

我有一个每秒都在运行的任务

代码语言:javascript
复制
final Handler handler = new Handler(); 
        Runnable runable = new Runnable() { 

            @Override 
            public void run() { 
                try{
                    //do your code here
                    //also call the same runnable 
                    handler.postDelayed(this, 1000);
                }
                catch (Exception e) {
                    // TODO: handle exception
                }
                finally{
                    //also call the same runnable 
                    handler.postDelayed(this, 1000); 
                }
            } 
        }; 
        handler.postDelayed(runable, 1000); 

我想在我的后台服务中每秒执行一些http post。我应该把我的代码放在BootCompleted类的什么地方?

EN

回答 1

Stack Overflow用户

发布于 2015-01-09 20:14:48

在服务类的onCreate方法中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27860272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档