当前位置:  开发笔记 > Android > 正文

如何每1分钟在后台运行服务

如何解决《如何每1分钟在后台运行服务》经验,为你挑选了1个好方法。

我需要在后台运行的每1分钟启动一次服务,即使我的应用程序没有运行

这是代码的一部分:`

我使用计时器每1分钟处理一次呼叫

    final Handler handler = new Handler();

    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {

        @Override
        public void run() {
                handler.post(new Runnable() {
                @SuppressWarnings("unchecked")
                public void run() {
                    try {

                        Intent intent = new Intent(ServiceMain.this, LocationMonitoringService.class);
                        startService(intent);


                        //   Toast.makeText(testServiceMain.this, "test", Toast.LENGTH_SHORT).show();


                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });


        }
    };


    timer.schedule(hourlyTask, 3, 1000 * 10);


}

"谢谢你



1> 小智..:

Android TimerTask示例
TimerTask表示任务将运行指定的时间,它将只运行一次或重复.
创建新的Class new TimerTask.
TimerTask有两种方法.

--> 
1 .scheduledExecutionTime() // Repeat Task
2 .schedule() //Only once
Timer singleTask = new Timer();
Timer repeatTask = new Timer();

int singleTaskInterval = 3000; // 3 sec
int repeatInterval = 10000; // 10 sec

 // this task for specified time only once it will run
singleTask.schedule(new TimerTask() {
@Override
public void run() {
// Here do something
// This task will run 3 sec only once.
}
}, 1000);

  // this task for specified time it will run Repeat
repeatTask.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Here do something
// This task will run every 10 sec repeat
}
}, 0, repeatInterval);

When your activity went to destroy or stop . you should cancel this task
--> --> 
@Override
protected void onDestroy(){
super.onDestroy();
if(singleTask != null){
singleTask.cancel();
}
if(repeatTask != null){
repeatTask.cancel();
}
}
Activity Code
/**
*
* @author vijayakumar
*
*/
public class AndroidMADQAActivity extends Activity {
/** Called when the activity is first created. */
Timer singleTask = new Timer();
Timer repeatTask = new Timer();
int singleTaskInterval = 3000; // 3 sec
int repeatInterval = 10000; // 10 sec
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
singleTask.schedule(new TimerTask() {
@Override
public void run() {
// Here do something
// This task will run 3 sec only once.
}
}, 1000);
repeatTask.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Here do something
// This task will run every 10 sec repeat
}
}, 0, repeatInterval);
}
@Override
protected void onDestroy(){
super.onDestroy();
if(singleTask != null){
singleTask.cancel();
}
if(repeatTask != null){
repeatTask.cancel();
}
}
}

推荐阅读
mobiledu2402851323
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有