Skip to content

Commit 0d25b9b

Browse files
committed
Decoupling UI and Network threads
We pass the communication through a BlockingQueue now.
1 parent b461e7a commit 0d25b9b

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

android/SnFChat/src/ro/ddalex/snfchat/MainActivity.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ro.ddalex.snfchat;
22

33
import java.util.ArrayList;
4+
import java.util.Timer;
5+
import java.util.TimerTask;
46

57
import android.os.Bundle;
68
import android.app.Activity;
@@ -19,14 +21,40 @@ public class MainActivity extends Activity {
1921

2022
private NetTalkerService service;
2123

24+
//private MainActivity getMAThis() { return this; }
25+
2226
@Override
2327
protected void onCreate(Bundle savedInstanceState) {
2428
super.onCreate(savedInstanceState);
2529
setContentView(R.layout.activity_main);
2630

2731
Intent i = new Intent(this, NetTalkerService.class);
2832
startService(i);
33+
34+
// get a timer to get the service and then pull updates
35+
final Timer t = new Timer();
36+
t.schedule(
37+
new TimerTask() {
38+
@Override
39+
public void run() {
40+
while ((service = NetTalkerService.getInstance()) == null)
41+
{
42+
try {
43+
Thread.sleep(10);
44+
} catch (InterruptedException e) {
45+
}
46+
}
2947

48+
while (! service.getMessageQueue().isEmpty() )
49+
{
50+
String s = service.getMessageQueue().poll();
51+
if (s != null)
52+
displayNetMessage(s);
53+
}
54+
}
55+
}, 10, 10);
56+
57+
3058
ListView listView = (ListView)findViewById(R.id.list);
3159
adapter=new ArrayAdapter<String>(this,
3260
android.R.layout.simple_list_item_1,
@@ -37,6 +65,7 @@ protected void onCreate(Bundle savedInstanceState) {
3765
@Override
3866
protected void onPause()
3967
{
68+
super.onPause();
4069
Intent i = new Intent(this, NetTalkerService.class);
4170
stopService(i);
4271
}
@@ -55,17 +84,9 @@ public void displayNetMessage(String message)
5584
}
5685

5786
public void sendNetMessage(View view)
58-
{
59-
60-
// busy wait here
61-
while ((service = NetTalkerService.getInstance()) == null)
62-
{
63-
try {
64-
Thread.sleep(10);
65-
} catch (InterruptedException e) {
66-
}
67-
}
68-
service.setActivity(this);
87+
{
88+
if (service == null)
89+
return;
6990

7091
EditText mEdit = (EditText) findViewById(R.id.editText1);
7192
String text = mEdit.getText().toString();

android/SnFChat/src/ro/ddalex/snfchat/NetTalkerService.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package ro.ddalex.snfchat;
22

3+
import java.util.concurrent.ArrayBlockingQueue;
4+
import java.util.concurrent.BlockingQueue;
5+
import java.util.concurrent.LinkedBlockingQueue;
6+
37
import android.app.Service;
48
import android.content.Intent;
59
import android.os.IBinder;
@@ -10,22 +14,45 @@ public class NetTalkerService extends Service {
1014
private static NetTalkerService sInstance = null;
1115
private String TAG = getClass().getSimpleName();
1216

17+
private BlockingQueue<String> incomingMessageQueue = new LinkedBlockingQueue<String>();
18+
19+
public BlockingQueue<String> getMessageQueue() { return incomingMessageQueue;}
20+
1321
private class NetListener extends Thread {
1422

23+
private int c = 0;
24+
25+
@Override
26+
public void run()
27+
{
28+
while(true) {
29+
incomingMessageQueue.offer("tata " + c);
30+
try {
31+
sleep(1000);
32+
} catch (InterruptedException e) {
33+
// TODO Auto-generated catch block
34+
e.printStackTrace();
35+
}
36+
c++;
37+
}
38+
}
1539
}
1640

1741
static public NetTalkerService getInstance(){
1842
return sInstance;
1943
}
2044

21-
private NetListener nl;
45+
private NetListener nl = null;
2246

2347
@Override
2448
public void onCreate() {
2549
super.onCreate();
2650
sInstance = this;
27-
28-
nl.start();
51+
if (nl == null)
52+
{
53+
nl = new NetListener();
54+
nl.start();
55+
}
2956
Log.i(TAG, "Service created");
3057
}
3158

@@ -35,19 +62,10 @@ public IBinder onBind(Intent arg0) {
3562
return null;
3663
}
3764

38-
// Specific code here
39-
private MainActivity mA = null;
40-
public void setActivity(MainActivity a)
41-
{
42-
mA = a;
43-
}
44-
4565
public void postMessage(String s)
4666
{
47-
if (mA == null)
48-
Log.e(TAG, "mA not available");
4967
Log.i(TAG, "Posting " + s);
50-
mA.displayNetMessage(s);
68+
incomingMessageQueue.offer(s);
5169
}
5270

5371
class NetReceiver{

0 commit comments

Comments
 (0)