Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Issah · Jun 11, 2015 at 04:22 PM · androidcrashinternetping

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
 
        }
      }



Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Issah · Jun 12, 2015 at 08:22 AM 0
Share

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)

avatar image dubbreak · Jun 12, 2015 at 02:12 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges