开发者

countDownTimer isn't working, why?

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

@Overri开发者_开发问答de
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?


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!


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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜