Skip to content

Commit 89c5389

Browse files
committed
fixed somewhat back problem
1 parent 252b8cb commit 89c5389

File tree

7 files changed

+160
-23
lines changed

7 files changed

+160
-23
lines changed

.gitignore

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,93 @@
1+
2+
# Created by https://www.gitignore.io/api/android
3+
# Edit at https://www.gitignore.io/?templates=android
4+
5+
### Android ###
6+
# Built application files
7+
*.apk
8+
*.ap_
9+
*.aab
10+
11+
# Files for the ART/Dalvik VM
12+
*.dex
13+
14+
# Java class files
15+
*.class
16+
17+
# Generated files
18+
bin/
19+
gen/
20+
out/
21+
release/
22+
23+
# Gradle files
24+
.gradle/
25+
build/
26+
27+
# Local configuration file (sdk path, etc)
28+
local.properties
29+
30+
# Proguard folder generated by Eclipse
31+
proguard/
32+
33+
# Log Files
34+
*.log
35+
36+
# Android Studio Navigation editor temp files
37+
.navigation/
38+
39+
# Android Studio captures folder
40+
captures/
41+
42+
# IntelliJ
143
*.iml
2-
.gradle
3-
/local.properties
4-
/.idea/caches
5-
/.idea/libraries
6-
/.idea/modules.xml
7-
/.idea/workspace.xml
8-
/.idea/navEditor.xml
9-
/.idea/assetWizardSettings.xml
10-
.DS_Store
11-
/build
12-
/captures
44+
.idea/workspace.xml
45+
.idea/tasks.xml
46+
.idea/gradle.xml
47+
.idea/assetWizardSettings.xml
48+
.idea/dictionaries
49+
.idea/libraries
50+
# Android Studio 3 in .gitignore file.
51+
.idea/caches
52+
.idea/modules.xml
53+
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
54+
.idea/navEditor.xml
55+
56+
# Keystore files
57+
# Uncomment the following lines if you do not want to check your keystore files in.
58+
#*.jks
59+
#*.keystore
60+
61+
# External native build folder generated in Android Studio 2.2 and later
1362
.externalNativeBuild
63+
64+
# Google Services (e.g. APIs or Firebase)
65+
# google-services.json
66+
67+
# Freeline
68+
freeline.py
69+
freeline/
70+
freeline_project_description.json
71+
72+
# fastlane
73+
fastlane/report.xml
74+
fastlane/Preview.html
75+
fastlane/screenshots
76+
fastlane/test_output
77+
fastlane/readme.md
78+
79+
# Version control
80+
vcs.xml
81+
82+
# lint
83+
lint/intermediates/
84+
lint/generated/
85+
lint/outputs/
86+
lint/tmp/
87+
# lint/reports/
88+
89+
### Android Patch ###
90+
gen-external-apklibs
91+
output.json
92+
93+
# End of https://www.gitignore.io/api/android

app/src/main/java/com/example/chatfull/ChatActivity.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class ChatActivity extends AppCompatActivity {
2323
User user;
2424
ArrayList<String> received_message = new ArrayList<>();
2525
SendMessage sender;
26+
MessageReceiveServer messageReceiveServer;
2627

2728
@Override
2829
protected void onCreate(Bundle savedInstanceState) {
@@ -38,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
3839
messageDisplay.setText("Connected to: " + user.getIpAddress() + ":" + user.getPort() + "\n");
3940
messageDisplay.append("Self: " + ShowInfoActivity.getSelfIpAddress() + ":" + ShowInfoActivity.getSelfPort());
4041

41-
new MessageReceiveServer(ShowInfoActivity.getSelfIpAddress(),ShowInfoActivity.getSelfPort(),this);
42+
messageReceiveServer = new MessageReceiveServer(ShowInfoActivity.getSelfIpAddress(),ShowInfoActivity.getSelfPort(),this);
4243
}
4344

4445
public void OnMsgSendBtnClick(View view){
@@ -61,4 +62,11 @@ public void run() {
6162
}
6263
});
6364
}
65+
66+
@Override
67+
protected void onDestroy() {
68+
super.onDestroy();
69+
sender.cancel(true);
70+
messageReceiveServer.onDestroy();
71+
}
6472
}

app/src/main/java/com/example/chatfull/ConnectToUserActivity.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ConnectToUserActivity extends AppCompatActivity {
1515
Button connectBtn;
1616
Client myClient;
1717
User user;
18+
boolean paused = false;
1819

1920
public void setUser(User user) {
2021
this.user = user;
@@ -34,6 +35,21 @@ protected void onCreate(Bundle savedInstanceState) {
3435
connectBtn = findViewById(R.id.connectBtn);
3536
}
3637

38+
@Override
39+
protected void onResume() {
40+
super.onResume();
41+
if(paused) {
42+
myClient.cancel(true);
43+
recreate();
44+
}
45+
}
46+
47+
@Override
48+
protected void onPause() {
49+
super.onPause();
50+
paused = true;
51+
}
52+
3753
public void connectBtnListener(View view){
3854
myClient = new Client(ipInput.getText().toString(),Integer.parseInt(portInput.getText().toString()),this);
3955
myClient.execute();

app/src/main/java/com/example/chatfull/MessageReceiveServer.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class MessageReceiveServer{
1313
int port;
1414
ChatActivity activity;
1515
ServerSocket serverSocket;
16+
boolean stop = false;
1617
int cnt = 0;
1718

1819
MessageReceiveServer(String ip_address, int port, ChatActivity activity){
@@ -29,7 +30,7 @@ private class MessageSocketServerThread extends Thread {
2930
public void run() {
3031
try {
3132
serverSocket = new ServerSocket(port);
32-
while (true) {
33+
while (stop == false) {
3334
cnt++;
3435
Log.e("Abar",""+cnt);
3536

@@ -55,5 +56,15 @@ public void run() {
5556
}
5657
}
5758
}
59+
public void onDestroy(){
60+
if(serverSocket != null){
61+
try {
62+
serverSocket.close();
63+
stop = true;
64+
} catch (IOException e) {
65+
e.printStackTrace();
66+
}
67+
}
68+
}
5869

5970
}

app/src/main/java/com/example/chatfull/SendMessage.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ protected String doInBackground(Void... arg0) {
4141
Log.e("SendMSG","ConnectHoyNai"+message);
4242
e.printStackTrace();
4343
}
44-
// Thread.interrupted();
4544
return "";
4645
}
4746

app/src/main/java/com/example/chatfull/Server.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class Server {
1616
String self_ip_address;
1717
int self_port;
1818
public User user;
19+
boolean stop = false;
1920

2021
public Server(ShowInfoActivity activity, String self_ip_address, int self_port) {
2122
this.activity = activity;
@@ -33,7 +34,7 @@ public void run() {
3334
// create ServerSocket using specified port
3435
serverSocket = new ServerSocket(self_port);
3536

36-
while (true) {
37+
while (stop == false) {
3738
// block the call until connection is created and return
3839
// Socket object
3940
Log.e("Server","InsideServer");
@@ -67,20 +68,26 @@ public void run() {
6768
activity.setConnected(true);
6869
}
6970
});
71+
stop = true;
7072
break;
7173
}
7274
} catch (IOException e) {
7375
// TODO Auto-generated catch block
7476
e.printStackTrace();
7577
}
76-
if (serverSocket != null) {
77-
try {
78-
Log.e("Server","Close Server");
79-
serverSocket.close();
80-
} catch (IOException e) {
81-
// TODO Auto-generated catch block
82-
e.printStackTrace();
83-
}
78+
onDestroy();
79+
}
80+
}
81+
82+
void onDestroy(){
83+
if (serverSocket != null) {
84+
try {
85+
Log.e("Server","Close Server");
86+
serverSocket.close();
87+
stop = true;
88+
} catch (IOException e) {
89+
// TODO Auto-generated catch block
90+
e.printStackTrace();
8491
}
8592
}
8693
}

app/src/main/java/com/example/chatfull/ShowInfoActivity.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ShowInfoActivity extends AppCompatActivity {
1717

1818
static final int selfPort = 8080;
1919
Server myServer;
20+
boolean paused = false;
2021

2122
public void setConnected(boolean connected) {
2223
this.connected = connected;
@@ -44,6 +45,21 @@ protected void onCreate(Bundle savedInstanceState) {
4445
Log.e("ShowActivity","CreatedServer");
4546
}
4647

48+
@Override
49+
protected void onResume() {
50+
super.onResume();
51+
if(paused) {
52+
myServer.onDestroy();
53+
recreate();
54+
}
55+
}
56+
57+
@Override
58+
protected void onPause() {
59+
super.onPause();
60+
paused = true;
61+
}
62+
4763
public static int getSelfPort() {
4864
return selfPort;
4965
}

0 commit comments

Comments
 (0)