我需要每隔10秒重复一次吐司。我该怎么做呢。
下面我添加一个简单的服务类代码:
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Repeat After 10 Sec", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
答案 0 :(得分:1)
今天我使用CountDownTimer和Service
完成了这项工作这是示例代码。
在服务中
MyTimer timer;
@Override
public void onCreate() {
Toast.makeText(this, "Repeat After 10 Sec", Toast.LENGTH_LONG).show();
timer = new MyTimer(200000, 10000);
}
倒计时课程
class MyTimer extends CountDownTimer {
// constructor for timer class
public MyTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
// this method called when timer is finished
@Override
public void onFinish() {
timer.start();
}
// this method is called for every iteration of time interval
@Override
public void onTick(long millisUntilFinished) {
//display toast here
Toast.makeText(context, "YOUR MESSAGE", Toast.LENGTH_LONG).show();
}
}