- Home /
Ping make my android app crash if no internet connection
Hi,
I am looking for a system which determine if internet is available when i launch my app. After searching on internet i have try many solution and on editor this one works :
IEnumerator CheckConnectionToMasterServer() {
Ping pingMasterServer = new Ping("74.125.224.72");
float startTime = Time.time;
while (!pingMasterServer.isDone && Time.time < startTime + 5.0f) {
yield return new WaitForSeconds(5.0f);
}
if(pingMasterServer.isDone && pingMasterServer.time > 2) {
Debug.Log ("IntenetON" );
} if(pingMasterServer.isDone && pingMasterServer.time<=1) {
Debug.Log ("IntenetOFF" );
}
}
When i build an apk for android there is no problem, ping works (cause i am connected to wifi).
If i launch with no internet connection my app instantly crash I have try to comment the while and always wait 5 seconds to check the ping result and its still crashing...
Anyone can solve this issue ? Thank you.
Answer by dubbreak · Jun 11, 2015 at 07:45 PM
Easy, you don't have a try/catch block around your method.
I didn't find the overload constructor for Ping(string), but I assume it's the same as creating a Ping class then doing Send(string) (i.e. it immediately attempts to ping the address provided in the string).
If you check the documentation for Ping.Send(string) it throws various exceptions you have to deal with (otherwise your program will crash).
Best guess is it's throwing the PingException since it can't resolve the address. Catch the exception, then do something that makes sense for your application (warn user can't ping or what have you).
IEnumerator CheckConnectionToMasterServer() {
try{
Ping pingMasterServer = new Ping("74.125.224.72");
float startTime = Time.time;
while (!pingMasterServer.isDone && Time.time < startTime + 5.0f) {
yield return new WaitForSeconds(5.0f);
}
if(pingMasterServer.isDone && pingMasterServer.time > 2) {
Debug.Log ("IntenetON" );
} if(pingMasterServer.isDone && pingMasterServer.time<=1) {
Debug.Log ("IntenetOFF" );
}
}
catch (exception e)
{
Debug.LogWarning("Couldn't ping: " + e.message);
}
}
Hi, Thank you for you help, i have a compilation error with your code : " Cannot yield a value in the body of a try block with a catch clause "
With finally the code compile fine but i have the same crash issue :(
IEnumerator CheckConnectionTo$$anonymous$$asterServer() {
try{
yield return new WaitForSeconds(10f);
Ping ping$$anonymous$$asterServer = new Ping("74.125.224.72");
float startTime = Time.time;
while (!ping$$anonymous$$asterServer.isDone && Time.time < startTime + 5.0f) {
yield return new WaitForSeconds(5.0f);
}
if(ping$$anonymous$$asterServer.isDone && ping$$anonymous$$asterServer.time > 2) {
Debug.Log ("IntenetON" );
} if(ping$$anonymous$$asterServer.isDone && ping$$anonymous$$asterServer.time<=1) {
Debug.Log ("IntenetOFF" );
}
}
finally
{
Debug.LogWarning("Couldn't ping: ");
}
}
Log cat result :
W/Activity$$anonymous$$anager( 534): Force finishing activity 1 com.xxx.xxx/com.prim e31.UnityPlayerNativeActivity I/DEBUG ( 182): I/DEBUG ( 182): $$anonymous$$bstone written to: /data/tombstones/tombstone_06 W/Activity$$anonymous$$anager( 534): Exception thrown during pause W/Activity$$anonymous$$anager( 534): android.os.DeadObjectException W/Activity$$anonymous$$anager( 534): at android.os.BinderProxy.transactNative(Native $$anonymous$$ethod) W/Activity$$anonymous$$anager( 534): at android.os.BinderProxy.transact(Binder.java:4 96)
That was just an example, not necessarily meant to compile.
If you put the try just around the ping it should be ok. Also you don't want finally. Finnally happens every time (whether it fails or not) so with your code it's always going to say "Couldn't ping: ". You need a catch. There is no point to having a try statement without a catch. You can, but it's bad practice.
So something like:
IEnumerator CheckConnectionTo$$anonymous$$asterServer() {
Ping ping$$anonymous$$asterServer = null;
try{
Ping ping$$anonymous$$asterServer = new Ping("74.125.224.72");
}
catch(exception e)
{
Debug.LogWarning("Couldn't ping: " + e.message);
return; //can't continue
}
//got here, can continue with logic
float startTime = Time.time;
while (!ping$$anonymous$$asterServer.isDone && Time.time < startTime + 5.0f) {
yield return new WaitForSeconds(5.0f);
}
if(ping$$anonymous$$asterServer.isDone && ping$$anonymous$$asterServer.time > 2) {
Debug.Log ("IntenetON" );
} if(ping$$anonymous$$asterServer.isDone && ping$$anonymous$$asterServer.time<=1) {
Debug.Log ("IntenetOFF" );
}
}
}
Your logic around how long it takes and internetOn/Off doesn't makes sense to me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200858im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detecting internet availability on Android 7 Answers
Android tablet crashes depending on texture compression 0 Answers
What might cause an Android app to freeze 1 Answer
Android level doesn't want to work after Application.LoadLevel 1 Answer
Unity 4.2.2 Crashes When Switching Target Platform to Android 2 Answers