Am trying yo use a TimerTask()
with a Handler()
associated with a postDelayed to update my notification running the method task every 1sec but I seem to be hitting a wall implementing it properly from the looks of things.
Here is a sample from my code:
Timer timer = new Timer();
TimerTask task = new TimerTask(){
@Override
public void run() {
updateNotificationUi();
}
};
timer.scheduleAtFixedRate(task, 0, 1000);
}
Then updateNotificationUi()
method code:
private void updateNotificationUi() {
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
if(isRunning) {
mNotificationBuilder.setContentTitle("Text").setContentText("This value here updates every 1 sec").setSmallIcon(R.drawable.ic_launcher);
mNotifyManager.notify(notificationID, mNotificationBuilder.build());
}
}
}, 1000);
}
The error I get when running my app is 12-22 07:37:20.556: E/AndroidRuntime(6555): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Any suggestions on how to fix this issue?