Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 4    Views: 84

benedettoapp
Lost but trying
Profile
Posts: 139
Reg: Jun 22, 2013
Italy
6,340
02/24/16 06:53 PM (8 years ago)

Plugin request: check internet connectivity

Sorry for referring to app inventor, but i'm coming from there, and it's easier if i show you. http://goo.gl/gRKmDA With the code you see i was able to tell the app before opening the screen to check for internet if there was than continue to open the app, if there is no internet than a message would tell the user to activate it or use wifi. because currently if i make an app that requires internet on the 1st screen and internet is off it will tell me: Something's not right error loading screen? the application had trouble understanding what to display with an option to hit ok, and once you hit ok it will say: webpage not available
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
02/25/16 05:50 AM (8 years ago)
I use these routines for my stuff, if I need to do things like that... Android: ///////////////////////////////////////////////////// private boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); BT_debugger.showIt(activityName + ":isNetworkAvailable = " + activeNetworkInfo); return activeNetworkInfo != null; } ///////////////////////////////////////////////////// iOS: -(BOOL) canReach { Reachability *r = [Reachability reachabilityWithHostName:@"m.google.com"]; NetworkStatus internetStatus = [r currentReachabilityStatus]; if(internetStatus == NotReachable){ // no connection return false; }else{ // connection return true; } } Hope this can help. Cheers! -- Smug
 
benedettoapp
Lost but trying
Profile
Posts: 139
Reg: Jun 22, 2013
Italy
6,340
like
02/25/16 06:28 AM (8 years ago)
Would you explain where would I put that and what does it do? for android.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
02/25/16 06:44 AM (8 years ago)
What does it do? I checks to see if you're connected to the internet. ;) Paste this in your plugin, outside of any other method in the class: private boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) this.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); BT_debugger.showIt(fragmentName + ":isNetworkAvailable = " + activeNetworkInfo); return activeNetworkInfo != null; } Then, somewhere in the "onCreateView" method, you would want to alert the user if they were not connected, or perhaps do something... then you would use an 'if' statement, to 'test' the connection... like this: boolean connectedToInternet = isNetworkAvailable(); if(connectedToInternet) { showToast("Connected to the Internet", "short"); } else { showToast("Not connected to the Internet", "short"); } If you are not comfortable working with code, you might ask Dusko, our resident Android 'gun for hire' if he is available to work with you on getting it done. He usually has reasonable rates and does a pretty quick job. Cheers! -- Smug If your project explodes in red (errors) it's probably because you'll need to 'import' header files for some of the system calls in the method. import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo;
 
benedettoapp
Lost but trying
Profile
Posts: 139
Reg: Jun 22, 2013
Italy
6,340
like
02/25/16 07:27 AM (8 years ago)
Ok thanks, good to know that there is a 'gun for hire'
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.