I want to send data to the server periodically, I\'m using background Service
for that, but I want to send when the data got updated, and updated data I\'m getting
Use multithreading instead it becomes much easier and you will get the same functionality.
mHandler = new Handler();
// Set a click listener for button
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCounter = 0;
/*
Runnable
Represents a command that can be executed. Often used to run code in a
different Thread.
Thread
A Thread is a concurrent unit of execution. It has its own call stack for
methods being invoked, their arguments and local variables. Each application
has at least one thread running when it is started, the main thread, in the
main ThreadGroup. The runtime keeps its own threads in the system thread group.
There are two ways to execute code in a new thread. You can either subclass
Thread and overriding its run() method, or construct a new Thread and pass a
Runnable to the constructor. In either case, the start() method must be
called to actually execute the new Thread.
*/
mRunnable = new Runnable() {
/*
public abstract void run ()
Starts executing the active part of the class' code. This method is
called when a thread is started that has been created with a class which
implements Runnable.
*/
@Override
public void run() {
// Do some task on delay
doTask();
}
};
/*
public final boolean postDelayed (Runnable r, long delayMillis)
Causes the Runnable r to be added to the message queue, to be run after the
specified amount of time elapses. The runnable will be run on the thread to
which this handler is attached. The time-base is uptimeMillis(). Time spent
in deep sleep will add an additional delay to execution.
*/
mHandler.postDelayed(mRunnable, (mInterval));
}
}); //use minterval to be the period in ms eg: private int mInterval = 4000;