English 中文(简体)
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

@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?

最佳回答

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





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签