android - 如何每 1 分钟在后台运行一次服务

标签 android service

我需要每 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);


}

` 谢谢

最佳答案

Android TimerTask 示例
TimerTask 表示任务将运行指定的时间,它只会运行一次或重复。
创建新类新 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();
        }
    }
}

关于android - 如何每 1 分钟在后台运行一次服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34330221/

相关文章:

service - 如何从蓝牙服务规范中识别ble的UUID?

c# - 澳洲邮政运费计算

windows - tcp 错误代码 10060 调用 WCF 服务

android - 如何将服务绑定(bind)到另一个 Activity ?

java - 嵌套LinearLayout仅显示第一个View

android - 如何在 android 中的 Activity 启动时从 sharedpreference 加载语言?

java - Android BLE BluetoothAdapter.LeScanCallback scanRecord 长度模糊

android - 所有Android广播Intent列表在哪里

android - 在 AppCompatActivity 的情况下不应用字体样式

Android JobIntentService - onStartCommand 与 onHandleWork