需要使用服务,每10秒后重复敬酒? [英] Need to repeat toast after every 10 sec using Service?

查看:168
本文介绍了需要使用服务,每10秒后重复敬酒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每10秒后重复吐司。我怎样才能做到这一点。

下面我添加了服务类的简单code:

 公共类的MyService扩展服务{@覆盖
公众的IBinder onBind(意向意图){
    返回null;
}@覆盖
公共无效的onCreate(){
    Toast.makeText(这一点,重复后10秒,Toast.LENGTH_LONG).show();
}@覆盖
公共无效的onDestroy(){
    Toast.makeText(这一点,服务已停止,Toast.LENGTH_LONG).show();
}@覆盖
公共无效调用onStart(意向意图,诠释startid){
    Toast.makeText(这一点,服务启动,Toast.LENGTH_LONG).show();
}


解决方案

今天,我已经做到了这一点使用CountDownTimer和服务

下面是样本code。

在服务

  MyTimer定时器;
@覆盖
公共无效的onCreate(){
   Toast.makeText(这一点,重复后10秒,Toast.LENGTH_LONG).show();
   定时器=新MyTimer(200000 10000);
}

countdowntimer类

 类MyTimer扩展CountDownTimer {    //构造Timer类
    公共MyTimer(长millisInFuture,长countDownInterval){
        超(mil​​lisInFuture,countDownInterval);    }    //此方法时计时器结束所谓的
    @覆盖
    公共无效onFinish(){        timer.start();
    }    //此方法被调用的时间间隔的每一次迭代
    @覆盖
    公共无效onTick(长millisUntilFinished){
        //显示这里敬酒
        Toast.makeText(背景下,您的信息,Toast.LENGTH_LONG).show();
    }
   }

I need to repeat Toast after every 10 second. How can I do this thing.

Below I add a simple code of the Service Class :

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();
}

解决方案

Today I have done this using CountDownTimer and Service

here is sample code.

In service

MyTimer timer;
@Override
public void onCreate() {
   Toast.makeText(this, "Repeat After 10 Sec", Toast.LENGTH_LONG).show();
   timer = new MyTimer(200000, 10000);
}

countdowntimer class

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();
    }
   }

这篇关于需要使用服务,每10秒后重复敬酒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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