Skip to content

Instantly share code, notes, and snippets.

@phpmaps
Created June 3, 2013 17:24
Show Gist options
  • Save phpmaps/5699732 to your computer and use it in GitHub Desktop.
Save phpmaps/5699732 to your computer and use it in GitHub Desktop.
Android polling example which gets triggered every 10 seconds.
protected void getLocationPeriodically() {
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Location l = (Location) msg.obj;
Toast.makeText(getApplicationContext(), "Latitude: " + String.valueOf(l.getLatitude()) + " Longitude: " + String.valueOf(l.getLongitude()),
Toast.LENGTH_LONG).show();
}
};
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
@Override
public void run() {
Location l = BasicReceiver.fieldLocation;
Message msg = new Message();
msg.obj = l;
mHandler.sendMessage(msg);
}
});
} catch (Exception e) {
}
}
}
}).start();
}
@r9software
Copy link

`mRunnable=new Runnable() {

                        @Override
                        public void run() {
                        	Location l = BasicReceiver.fieldLocation;
                        	Message msg = new Message();
                        	msg.obj = l;
                        	mHandler.sendMessage(msg);
                                     mHandler.removeCallbacks(mRunnable);
                                    mHandler.postAtTime(mRunnable,10000);
                        }
                    }`

to execute just call
mHandler.post(mRunnable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment