This is because you are trying to acccess the UI on a seperate thread. You will need to use an AsyncTask
thread which will enable you to periodically access the UI thread.
For example:
private class ExampleThread extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {}
@Override
protected void doInBackground(Void... params) {
while(!isCancelled()) { // Keep going until cancelled
try {
Thread.sleep(100); // Delay 100 milliseconds
} catch (InterruptedException e) {
Thread.interrupted();
}
publishProgress(); // Run onProgressUpdate() method
if(isCancelled()) break; // Escape early if cancel() is called
}
}
@Override
protected void onPostExecute(Void... params) {}
@Override
protected void onProgressUpdate(Void... params) {
// Here you can access the UI thread
progBar.setMax(settPref.getInt(max, defaultValue));
progBar.setProgress(settPref.getInt(progress, defaultValue));
}
}
To start the thread:
ExampleThread thread = new ExampleThread();
thread.execute();
To stop the thread:
thread.cancel();
More information and examples with AsyncTask can be found on this question.