Lecture Notes. Thursday 10
Concurrency – Message Passing – Handler
<?xml version="1.0" encoding="utf-8"?>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000ff"
android:id="@+id/txtMsg"
android:textStyle="bold"
android:textSize="14sp"/>
ProgressBar android:id="@+id/ProgressBar01Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
/>
ProgressBar android:id="@+id/ProgressBar02Circular"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
DigitalClock android:text="@+id/DigitalClock01"
android:id="@+id/DigitalClock01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"</DigitalClock
EditText android:hint="enter user name"
android:id="@+id/txtUser"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Button android:text="GO" android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout
package ucr.conc2;
import . . .
public class Main extends Activity {
Button btnGo;
EditText txtUser;
TextView txtMsg;
ProgressBar progBarH, progBarC;
Handler myHandler1 = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.arg1 >= progBarH.getMax()-1){
txtMsg.setText("All done ... 100% completed");
progBarC.setVisibility(View.INVISIBLE);
progBarH.setVisibility(View.INVISIBLE);
progBarC.getLayoutParams().height = 0;
progBarH.getLayoutParams().height = 0;
}
else {
String text = "Patience\n. We have already processed: "
+ msg.arg1
+ "% of data\n";
txtMsg.setText(text);
progBarH.setProgress(msg.arg1);
}
}//handleMessage
};//Handler
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// plumbing
txtMsg = (TextView)findViewById(R.id.txtMsg);
txtUser = (EditText)findViewById(R.id.txtUser);
progBarH = (ProgressBar)findViewById(R.id.ProgressBar01Horizontal);
progBarC = (ProgressBar)findViewById(R.id.ProgressBar02Circular);
progBarH.setMax(100); //range is 0..100
btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text = txtUser.getText().toString();
text = "Hola\n" + text + "\n" + (new Date()).toLocaleString();
txtUser.setText(text);
//progBarH.setProgress((new Date()).getSeconds());
}
});
}//onCreate
@Override
protected void onStart() {
super.onStart();
Thread backgWorker1 = new Thread(new Runnable() {
@Override
public void run() {
// heavy duty computing goes here ....
for (int i=1; i <= 100; i++){
// fake busy work
if (i % 10 ==0){
Message msg = myHandler1.obtainMessage();
msg.arg1 = i;
msg.what = 1;
myHandler1.sendMessage(msg);
}
try {
Thread.sleep(100);
Log.e("<PROGRESS>", "prog. value: " + i);
} catch (InterruptedException e) {
Log.e("<PROBLEMS>", e.getMessage());
}
}
}
});
backgWorker1.start();
progBarH.setProgress(0);
}//onStart
}//Main
DEMO3. Posting – Runnables
<?xml version="1.0" encoding="utf-8"?>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000ff"
android:id="@+id/txtMsg"
android:textStyle="bold"
android:textSize="14sp"/>
ProgressBar android:id="@+id/ProgressBar01Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
/>
ProgressBar android:id="@+id/ProgressBar02Circular"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
DigitalClock android:text="@+id/DigitalClock01"
android:id="@+id/DigitalClock01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"</DigitalClock
EditText android:hint="enter user name"
android:id="@+id/txtUser"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Button android:text="GO" android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Button android:text="STOP" android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout
package ucr.concurrency3;
import java.util.Date;
import . . .
public class Main extends Activity {
Button btnGo, btnStop;
EditText txtUser;
TextView txtMsg;
ProgressBar progBarH, progBarC;
boolean keepWorking = true;
int sharedGlobalVar = 0;
Handler myHandler1 = new Handler();
Runnable foregWorker = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// UI work on behalf of background worker
if (sharedGlobalVar >= progBarH.getMax()-1){
txtMsg.setText("All done! - 100% completed");
dismissProgressBars();
}
else {
txtMsg.setText("We have already processed " + sharedGlobalVar + "% of data");
progBarH.setProgress(sharedGlobalVar);
}
}
};
// //////////////////////////////////////////////////////////////////////
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// plumbing
txtMsg = (TextView)findViewById(R.id.txtMsg);
txtUser = (EditText)findViewById(R.id.txtUser);
progBarH = (ProgressBar)findViewById(R.id.ProgressBar01Horizontal);
progBarC = (ProgressBar)findViewById(R.id.ProgressBar02Circular);
progBarH.setMax(100); //range is 0..100
btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text = txtUser.getText().toString();
text = "Hola\n" + text + "\n" + (new Date()).toLocaleString();
txtUser.setText(text);
}
});
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
keepWorking = false;
dismissProgressBars();
}
});
}//onCreate
@Override
protected void onStart() {
super.onStart();
Thread backgWorker = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// busy work goes here...
for (int i=1; (i<=100)&keepWorking ; i++){
Log.e("<BACK>", "value of i is: " + i);
if (i%10==0){
sharedGlobalVar = i;
myHandler1.post(foregWorker);
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
Log.e("<ERROR>", e.getMessage() );
}
}
}
});
backgWorker.start();
}//onStart
private void dismissProgressBars() {
progBarC.setVisibility(View.INVISIBLE);
progBarC.getLayoutParams().height=0;
progBarH.setVisibility(View.INVISIBLE);
progBarH.getLayoutParams().height=0;
}// dismissProgressBars
}//Main
Demo4 – AsyncTask
<?xml version="1.0" encoding="utf-8"?>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000ff"
android:id="@+id/txtMsg"
android:textStyle="bold"
android:textSize="14sp"/>
DigitalClock android:text="@+id/DigitalClock01"
android:id="@+id/DigitalClock01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
EditText android:hint="enter user name"
android:id="@+id/txtUser"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Button android:text="SLOW" android:id="@+id/btnSlow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Button android:text="FAST" android:id="@+id/btnFast"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout
package ucr.concurrent4;
import . . .
public class Main extends Activity {
Button btnFast, btnSlow;
EditText txtUser;
TextView txtMsg;
boolean keepWorking = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// plumbing
txtMsg = (TextView)findViewById(R.id.txtMsg);
txtUser = (EditText)findViewById(R.id.txtUser);
btnSlow= (Button) findViewById(R.id.btnSlow);
btnSlow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text = txtUser.getText().toString();
text = "Hola\n" + text + "\n" + (new Date()).toLocaleString();
txtUser.setText(text);
keepWorking = true;
String[] myParams = {"a", "b", "c"};
new VerySlowTask().execute(myParams);
}
});
btnFast = (Button) findViewById(R.id.btnFast);
btnFast.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
txtUser.setText("Quick\n" + (new Date().toLocaleString()));
}
});
}//onCreate
// private class VerySlowTask extends AsyncTask<Params, Progress, Result>
// ------
private class VerySlowTask extends AsyncTask<String, Long, Integer> {
final ProgressDialog dialog = new ProgressDialog(Main.this);
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
if (dialog.isShowing()){
dialog.dismiss();
}
}//onPostExecute
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setTitle("Slow work...");
dialog.setMessage("working ...");
dialog.show();
}//onPreExecute
@Override
protected void onProgressUpdate(Long... values) {
super.onProgressUpdate(values);
try {
txtUser.setText("Returned value: "
+ values[0] + " - "
+ values[1] + " - " + values[2] );
dialog.setMessage("working ... " + values[0]);
if (!dialog.isShowing()){
keepWorking = false;
txtUser.setText("Stopped...");
}
} catch (Exception e) {
Log.e("<ERROR>", e.getMessage());
}
}//onProgressUpdate
@Override
//protected Result doInBackground(Params... params) {
protected Integer doInBackground(String... params) {
Log.v("<doInBackground>", params[0] + ","
+ params[1] +"," + params[2]);
for (Long i=1L; (i<=50) & (keepWorking); i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Log.e("<ERROR>", e.getMessage());
}
publishProgress(i,222L,333L); //222,333 superfluous-debuggin }
publishProgress(0L, 0L, 0L);
return null;
}//doInBackground
}//VerySlowTask
}//Main