Add more error handling

Fix showing title bar after switching apps.
pull/1/head
Tindy X 2021-03-06 16:38:55 +08:00
parent 23438b3f1d
commit 815cf3cadf
No known key found for this signature in database
GPG Key ID: C6AD413169968D58
3 changed files with 45 additions and 25 deletions

View File

@ -555,6 +555,7 @@ class MainActivity : AppCompatActivity() {
byteArrayOf(23, 'C'.toByte(), 'O'.toByte(), 'N'.toByte()).copyInto(buffer)
selfAddress.toByteArray().copyInto(buffer, 4)
serverPort.toString().toByteArray().copyInto(buffer, 4 + 15)
try {
val socket = DatagramSocket()
val packet = DatagramPacket(buffer, buffer.size)
socket.apply {
@ -562,6 +563,9 @@ class MainActivity : AppCompatActivity() {
send(packet)
close()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
@ -572,11 +576,12 @@ class MainActivity : AppCompatActivity() {
if (mTCPMode) {
try {
mTCPSocket.getOutputStream().write(buffer)
mTCPSocket.close()
} catch (e: Exception) {
e.printStackTrace()
}
mTCPSocket.close()
} else {
try {
val socket = DatagramSocket()
val packet = DatagramPacket(buffer, buffer.size)
socket.apply {
@ -584,6 +589,9 @@ class MainActivity : AppCompatActivity() {
send(packet)
close()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
@ -599,6 +607,7 @@ class MainActivity : AppCompatActivity() {
e.printStackTrace()
}
} else {
try {
val socket = DatagramSocket()
val packet = DatagramPacket(buffer, buffer.size)
socket.apply {
@ -606,6 +615,9 @@ class MainActivity : AppCompatActivity() {
send(packet)
close()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
@ -619,6 +631,7 @@ class MainActivity : AppCompatActivity() {
val buffer = ByteArray(12)
byteArrayOf(11, 'P'.toByte(), 'I'.toByte(), 'N'.toByte()).copyInto(buffer)
ByteBuffer.wrap(buffer, 4, 8).putLong(SystemClock.elapsedRealtimeNanos())
try {
val socket = DatagramSocket()
val packet = DatagramPacket(buffer, buffer.size)
socket.apply {
@ -626,6 +639,9 @@ class MainActivity : AppCompatActivity() {
send(packet)
close()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun sendTCPPing() {

View File

@ -12,5 +12,7 @@
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>

View File

@ -12,5 +12,7 @@
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>