Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
KGregPFerenc
AndroidOnlabKviz2018
Commits
c99a1b7c
Commit
c99a1b7c
authored
Apr 07, 2018
by
fpeterfalvi
Browse files
Two-way communication in GameActivity
parent
9991f998
Changes
4
Hide whitespace changes
Inline
Side-by-side
KvizClient/app/src/main/java/onlab/kvizclient/GameActivity.java
View file @
c99a1b7c
...
...
@@ -4,6 +4,9 @@ import android.os.Handler;
import
android.support.v7.app.AppCompatActivity
;
import
android.os.Bundle
;
import
android.util.Log
;
import
android.view.View
;
import
android.widget.Button
;
import
android.widget.EditText
;
import
android.widget.TextView
;
import
org.w3c.dom.Text
;
...
...
@@ -18,7 +21,11 @@ import java.net.UnknownHostException;
public
class
GameActivity
extends
AppCompatActivity
{
TextView
questionText
;
private
TextView
questionText
;
private
BufferedReader
input
;
private
PrintWriter
output
;
Handler
updateConversationHandler
;
...
...
@@ -28,8 +35,25 @@ public class GameActivity extends AppCompatActivity {
setContentView
(
R
.
layout
.
activity_game
);
questionText
=
(
TextView
)
findViewById
(
R
.
id
.
QuestionTextView
);
final
EditText
answerEditText
=
(
EditText
)
findViewById
(
R
.
id
.
AnswerEditText
);
Button
sendAnswerButton
=
(
Button
)
findViewById
(
R
.
id
.
SendAnswerButton
);
updateConversationHandler
=
new
Handler
();
try
{
input
=
new
BufferedReader
(
new
InputStreamReader
(
ServerHolder
.
getSocket
().
getInputStream
()));
output
=
new
PrintWriter
(
new
BufferedWriter
(
new
OutputStreamWriter
(
ServerHolder
.
getSocket
().
getOutputStream
())),
true
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
sendAnswerButton
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
view
)
{
output
.
println
(
answerEditText
.
getText
());
}
});
CommunicationThread
commThread
=
new
CommunicationThread
();
Thread
newthread
=
new
Thread
(
commThread
);
newthread
.
start
();
...
...
@@ -37,20 +61,6 @@ public class GameActivity extends AppCompatActivity {
class
CommunicationThread
implements
Runnable
{
private
BufferedReader
input
;
private
PrintWriter
output
;
public
CommunicationThread
()
{
try
{
input
=
new
BufferedReader
(
new
InputStreamReader
(
ServerHolder
.
getSocket
().
getInputStream
()));
output
=
new
PrintWriter
(
new
BufferedWriter
(
new
OutputStreamWriter
(
ServerHolder
.
getSocket
().
getOutputStream
())),
true
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
public
void
run
()
{
while
(!
Thread
.
currentThread
().
isInterrupted
())
{
try
{
...
...
KvizClient/app/src/main/res/layout/activity_game.xml
View file @
c99a1b7c
...
...
@@ -6,10 +6,30 @@
android:layout_height=
"match_parent"
tools:context=
"onlab.kvizclient.GameActivity"
>
<
TextView
<
LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:id=
"@+id/QuestionTextView"
android:text=
"Ide jön a kérdés"
/>
android:orientation=
"vertical"
>
<TextView
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:id=
"@+id/QuestionTextView"
android:text=
"Ide jön a kérdés"
/>
<EditText
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:id=
"@+id/AnswerEditText"
android:text=
"Ide írd a választ"
/>
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:id=
"@+id/SendAnswerButton"
android:text=
"Send"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
KvizServer/app/src/main/java/onlab/kvizserver/GameActivity.java
View file @
c99a1b7c
...
...
@@ -2,10 +2,14 @@ package onlab.kvizserver;
import
android.content.Context
;
import
android.content.SharedPreferences
;
import
android.os.Handler
;
import
android.support.v7.app.AppCompatActivity
;
import
android.os.Bundle
;
import
android.util.Log
;
import
android.util.TypedValue
;
import
android.view.View
;
import
android.widget.Button
;
import
android.widget.LinearLayout
;
import
android.widget.TextView
;
import
java.io.BufferedReader
;
...
...
@@ -33,6 +37,11 @@ public class GameActivity extends AppCompatActivity {
private
List
<
PrintWriter
>
outputs
=
new
ArrayList
<>();
private
int
numberOfPlayers
;
final
List
<
TextView
>
playerAnswerTextViews
=
new
ArrayList
<>();
Handler
updateConversationHandler
;
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
...
...
@@ -41,6 +50,8 @@ public class GameActivity extends AppCompatActivity {
Bundle
extras
=
getIntent
().
getExtras
();
String
questionFileName
=
extras
.
getString
(
"QUESTION_FILE_NAME"
,
null
);
updateConversationHandler
=
new
Handler
();
BufferedReader
buffreader
;
try
{
if
(
questionFileName
==
null
)
{
...
...
@@ -60,6 +71,18 @@ public class GameActivity extends AppCompatActivity {
Button
nextQuestionBtn
=
(
Button
)
findViewById
(
R
.
id
.
NextQuestion
);
final
TextView
questionText
=
(
TextView
)
findViewById
(
R
.
id
.
QuestionText
);
final
TextView
correctAnswerText
=
(
TextView
)
findViewById
(
R
.
id
.
CorrectAnswerText
);
final
LinearLayout
playerAnswersLinearLayout
=
(
LinearLayout
)
findViewById
(
R
.
id
.
PlayerAnswers
);
numberOfPlayers
=
ClientHolder
.
size
();
for
(
int
i
=
0
;
i
<
numberOfPlayers
;
i
++)
{
final
TextView
playerAnswerTextView
=
new
TextView
(
this
);
playerAnswerTextView
.
setText
(
"The answer of the "
+
Integer
.
toString
(
i
+
1
)
+
". player: "
);
playerAnswerTextView
.
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
30
);
playerAnswersLinearLayout
.
addView
(
playerAnswerTextView
);
playerAnswerTextViews
.
add
(
playerAnswerTextView
);
}
try
{
for
(
int
i
=
0
;
i
<
ClientHolder
.
size
();
i
++)
{
...
...
@@ -77,17 +100,72 @@ public class GameActivity extends AppCompatActivity {
@Override
public
void
onClick
(
View
view
)
{
if
(
questionIndex
<
questions
.
size
())
{
String
questionString
=
questions
.
get
(
questionIndex
).
getQuestionText
();
GuessQuestion
question
=
questions
.
get
(
questionIndex
);
String
questionString
=
question
.
getQuestionText
();
questionText
.
setText
(
questionString
);
correctAnswerText
.
setText
(
"Correct answer: "
+
question
.
getCorrectAnswer
());
for
(
int
i
=
0
;
i
<
outputs
.
size
();
i
++)
{
playerAnswerTextViews
.
get
(
i
).
setText
(
"The answer of the "
+
Integer
.
toString
(
i
+
1
)
+
". player: "
);
outputs
.
get
(
i
).
println
(
questionString
);
}
questionIndex
++;
}
else
{
questionText
.
setText
(
"No more questions."
);
correctAnswerText
.
setText
(
""
);
}
}
});
for
(
int
i
=
0
;
i
<
numberOfPlayers
;
i
++)
{
CommunicationThread
commThread
=
new
CommunicationThread
(
i
);
Thread
newthread
=
new
Thread
(
commThread
);
newthread
.
start
();
}
}
class
CommunicationThread
implements
Runnable
{
private
int
index
;
private
BufferedReader
input
;
private
PrintWriter
output
;
public
CommunicationThread
(
int
index
)
{
this
.
index
=
index
;
input
=
inputs
.
get
(
index
);
output
=
outputs
.
get
(
index
);
}
public
void
run
()
{
while
(!
Thread
.
currentThread
().
isInterrupted
())
{
try
{
String
read
=
input
.
readLine
();
if
(
read
!=
null
)
{
Log
.
d
(
"GameActivity"
,
"Klienstől kapott üzenet: "
+
read
);
updateConversationHandler
.
post
(
new
GameActivity
.
updateUIThread
(
index
,
read
));
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
class
updateUIThread
implements
Runnable
{
String
msg
;
int
index
;
public
updateUIThread
(
int
index
,
String
msg
)
{
this
.
msg
=
msg
;
this
.
index
=
index
;
}
@Override
public
void
run
()
{
playerAnswerTextViews
.
get
(
index
).
setText
(
"The answer of the "
+
Integer
.
toString
(
index
+
1
)
+
". player: "
+
msg
);
}
}
}
KvizServer/app/src/main/res/layout/activity_game.xml
View file @
c99a1b7c
...
...
@@ -18,6 +18,21 @@
android:id=
"@+id/QuestionText"
android:textSize=
"30sp"
/>
<TextView
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"Correct answer:"
android:id=
"@+id/CorrectAnswerText"
android:textSize=
"30sp"
/>
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:orientation=
"vertical"
android:id=
"@+id/PlayerAnswers"
>
</LinearLayout>
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment