Created
December 4, 2012 01:23
-
-
Save bpascazio/4199669 to your computer and use it in GitHub Desktop.
handler sample with bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bytefly.handlersample; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.app.Activity; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ProgressBar; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
private static final int SUCCESS = 0; | |
private static final int FAILURE = 1; | |
Activity act; | |
boolean isOk = true; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
act = this; | |
Button.OnClickListener myListener = new Button.OnClickListener() { | |
public void onClick(View v) { | |
hitTheNetwork(); | |
} | |
}; | |
Button myButton = (Button) findViewById(R.id.button1); | |
myButton.setOnClickListener(myListener); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
public void showProgress() { | |
ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1); | |
pb.setProgress(0); | |
pb.setMax(5); | |
pb.setVisibility(View.VISIBLE); | |
} | |
public void hideProgress() { | |
ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1); | |
pb.setVisibility(View.INVISIBLE); | |
} | |
public void updateProgress(int val) { | |
ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1); | |
pb.setProgress(val); | |
} | |
public void hitTheNetwork() { | |
// runs in the main thread | |
showProgress(); | |
Runnable r = new Runnable() { | |
public void run() { | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
} | |
updateProgress(1); | |
try { | |
Thread.sleep(3000); | |
} catch (InterruptedException e) { | |
} | |
updateProgress(4); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
} | |
updateProgress(5); | |
Message msg = new Message(); | |
if (isOk) { | |
msg.what=SUCCESS; | |
myHandler.sendMessage(msg); | |
isOk = false; | |
} else { | |
msg.what=FAILURE; | |
msg.obj = "Network failed 400"; | |
myHandler.sendMessage(msg); | |
isOk = true; | |
} | |
} | |
}; | |
Thread thr = new Thread(r); | |
thr.start(); | |
} | |
private Handler myHandler = new Handler() { | |
public void handleMessage(Message msg) { | |
hideProgress(); | |
switch (msg.what) { | |
case SUCCESS: | |
break; | |
case FAILURE: | |
Toast t = Toast.makeText(act, (String)(msg.obj), Toast.LENGTH_SHORT); | |
t.show(); | |
break; | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment