倒计时计时器不起作用,为什么?

发布于 2024-11-10 06:47:39 字数 1664 浏览 0 评论 0原文

我有一个服务 B,它以固定的时间间隔发送特定数量的消息。 该服务是从另一个服务 A 调用的。 服务 A 中使用的代码是

@Override
public void onStart (Intent intent,int startid)
{
    Toast.makeText(this, "Service A Running onStart", Toast.LENGTH_LONG).show();

    Thread MessagesThread = new Thread(new Runnable()
    {
        public void run()
        {
            ApplicationPreferences AppPrefs = new ApplicationPreferences(getApplicationContext());
            int NumberOfMessagesToSend = Integer.parseInt(AppPrefs.getNumberOfMessagesToSend());
            int NumberOfSentMessages;

            for (NumberOfSentMessages = 0 ; NumberOfSentMessages < NumberOfMessagesToSend; NumberOfSentMessages++ )
            {startServiceB();
             }
        }
    });
    MessagesThread.start();
}

public void startServiceB()
{
    final Intent sendingMessages = new Intent(this, ServiceB.class);
    startService(sendingMessages);
}

toast,用于跟踪正在发生的事情 服务 B 中的代码如下所示

@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);
    Toast.makeText(getApplicationContext(), "Service B at start ", Toast.LENGTH_LONG).show();
    new CountDownTimer(30000,1000)
    {
        public void onTick (long millisUntilFinished) {}

        public void onFinish()
        {
            showToast();
        }
    }.start();
}

showToast() 函数如下

public void showToast()
{
    Toast.makeText(getApplicationContext(), "Service B in timer", Toast.LENGTH_LONG).show();
}

正如我所说,我使用 toast 来跟踪发生的情况。问题是运行它时,我得到第一个吐司(启动时的服务 B)10 次,然后第二个吐司(计时器中的服务 B)10 次,两者之间没有时间。

我如何让每个 toast 每 30 秒出现一次?

I have a service B that sends a specific number of messages in a fixed interval.
this service is called from another service A.
the code used in service A is

@Override
public void onStart (Intent intent,int startid)
{
    Toast.makeText(this, "Service A Running onStart", Toast.LENGTH_LONG).show();

    Thread MessagesThread = new Thread(new Runnable()
    {
        public void run()
        {
            ApplicationPreferences AppPrefs = new ApplicationPreferences(getApplicationContext());
            int NumberOfMessagesToSend = Integer.parseInt(AppPrefs.getNumberOfMessagesToSend());
            int NumberOfSentMessages;

            for (NumberOfSentMessages = 0 ; NumberOfSentMessages < NumberOfMessagesToSend; NumberOfSentMessages++ )
            {startServiceB();
             }
        }
    });
    MessagesThread.start();
}

public void startServiceB()
{
    final Intent sendingMessages = new Intent(this, ServiceB.class);
    startService(sendingMessages);
}

the toasts are to keep track of what is happening
The code in service B is as follow

@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);
    Toast.makeText(getApplicationContext(), "Service B at start ", Toast.LENGTH_LONG).show();
    new CountDownTimer(30000,1000)
    {
        public void onTick (long millisUntilFinished) {}

        public void onFinish()
        {
            showToast();
        }
    }.start();
}

the showToast() function is as follow

public void showToast()
{
    Toast.makeText(getApplicationContext(), "Service B in timer", Toast.LENGTH_LONG).show();
}

As I said I am using the toasts to keep track of what's happening. the problem is when running it, i am getting the first toast (service B at start) 10 times consequently then the second one (service B in timer) 10 times consequently with no time between them.

how do i make each of this toasts appear once every 30 seconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢你一世 2024-11-17 06:47:39

好的,所以最终的答案可能是这样的:
仅调用一次 B 服务,其中我们将有一个以 30 秒的间隔循环的处理程序。

服务 B 代码:

 int loop = 5;
    int counter = 0;
Handler myHandler;
Runnable run;

    @Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);
        Toast.makeText(getApplicationContext(), "Service B at start ", Toast.LENGTH_LONG).show();
        myHandler = new Handler();
        run = new Runnable()
        {
         public void run() 
         {
          if (counter<loop){
           showToast(); 
           counter++;   
          } else {
            myHandler.removeCallbacks(run);
          }
         }
       };
       myHandler.postDelayed(run, 30000);

    }

我希望这对其他人也有帮助!

Ok, so the final answer could be something like this:
Call only once the B service and in it we will have the handler that will loop at an interval of 30 seconds..

Service B code:

 int loop = 5;
    int counter = 0;
Handler myHandler;
Runnable run;

    @Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);
        Toast.makeText(getApplicationContext(), "Service B at start ", Toast.LENGTH_LONG).show();
        myHandler = new Handler();
        run = new Runnable()
        {
         public void run() 
         {
          if (counter<loop){
           showToast(); 
           counter++;   
          } else {
            myHandler.removeCallbacks(run);
          }
         }
       };
       myHandler.postDelayed(run, 30000);

    }

I hope this helps someone else too!

汹涌人海 2024-11-17 06:47:39

如果您想每 30 秒吐司一次,则可以使用处理程序来完成:

 Handler myHandler = new Handler();
Runnable run = new Runnable()
{
    public void run() 
    {
        showToast();                 
    }
};
myHandler.postDelayed(run, 30000);

如果您对此有疑问,请在此处发帖,我会尽力帮助您。

If you want to make a toast every 30 seconds than you can do it by using a handler:

 Handler myHandler = new Handler();
Runnable run = new Runnable()
{
    public void run() 
    {
        showToast();                 
    }
};
myHandler.postDelayed(run, 30000);

If you have problem with this just post here and I will try to help you..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文