Skip to content

Commit 5bafd20

Browse files
committed
added an Asyncronous Task to fetch the openCV results every 1 sec and do soemthign with them, in this case it just gets the results, and on post execute in the UI thread, shows teh user what the results are. fixes cesine#16
1 parent a88f489 commit 5bafd20

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

android/src/com/androidmontreal/opencv/AndroidOpenCVforHackathonsActivity.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,25 @@
1515
*/
1616
package com.androidmontreal.opencv;
1717

18-
1918
import android.app.Activity;
2019
import android.hardware.Camera;
2120
import android.hardware.Camera.PictureCallback;
2221
import android.util.Log;
2322
import android.view.View;
2423
import android.widget.Button;
2524
import android.widget.TextView;
25+
import android.os.AsyncTask;
2626
import android.os.Bundle;
2727

2828

2929
public class AndroidOpenCVforHackathonsActivity extends Activity implements PictureCallback
3030
{
3131
private static final String TAG = "OpenCVforHackathons";
3232

33+
// OpenCV results view
34+
private TextView textView1;
35+
private boolean stopUpdatingInBackground = false;
36+
3337

3438
/** Called when the activity is first created. */
3539
@Override
@@ -43,10 +47,41 @@ public void onCreate(Bundle savedInstanceState)
4347
* function.
4448
*/
4549
setContentView(R.layout.main);
46-
TextView tv = (TextView) findViewById(R.id.textview1);
47-
tv.setText( stringFromJNI() );
50+
textView1 = (TextView) findViewById(R.id.textview1);
51+
textView1.setText( stringFromJNI() );
4852

4953
}
54+
public void getOpenCVResult(View view){
55+
UpdateFromOpenCVTask getOpenCVResults = new UpdateFromOpenCVTask();
56+
getOpenCVResults.execute(new String[] { "in execute" });
57+
58+
}
59+
private class UpdateFromOpenCVTask extends AsyncTask<String, Void, String> {
60+
@Override
61+
protected String doInBackground(String... urls) {
62+
Log.d(TAG,"Pausing 1 sec before calling again.");
63+
try {
64+
new Thread().sleep(1000);
65+
} catch (InterruptedException e) {
66+
e.printStackTrace();
67+
}
68+
if(!(stopUpdatingInBackground)){
69+
// Loop every 1 sec
70+
getOpenCVResult(textView1);
71+
}
72+
String response = "Hi from the AsyncThread";
73+
response = ((AndroidOpenCVforHackathonsApp) getApplication()).getLastMessage();
74+
return response;
75+
}
76+
@Override
77+
protected void onPostExecute(String result) {
78+
/*
79+
* The result comes from the above doInBackground method,
80+
* but this runs on the UI thread and so we can set a field in the UI.
81+
*/
82+
textView1.setText(result);
83+
}
84+
}
5085

5186
@Override
5287
protected void onDestroy() {
@@ -63,13 +98,16 @@ public void onLowMemory() {
6398
@Override
6499
protected void onPause() {
65100
Log.d(TAG, "====onPause====");
66-
super.onPause();
101+
stopUpdatingInBackground = true;
102+
super.onPause();
67103
}
68104

69105
@Override
70106
protected void onResume() {
71107
Log.d(TAG, "==onResume==");
72108
super.onResume();
109+
stopUpdatingInBackground = false;
110+
getOpenCVResult(textView1);
73111
}
74112

75113
@Override

0 commit comments

Comments
 (0)