Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Better way to check internet connection? — Gideros Forum

Better way to check internet connection?

rrraptorrrraptor Member
edited October 2016 in General questions
Hello!
I was wondering how can i implement native share button to my game, and i found this thread. This method still works.
Now i need to check if player have internet connection or not, i created same as for share class in android studio:
package com.giderosmobile.android;
 
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
 
public class Connection {
 
    public static boolean isNetworkAvailable(Activity activity) {
        Context context = activity.getApplicationContext();
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
 
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }
 
}
And simple class (well, i don't realy need a class, just for test):
local bridge = native.getClass("com.giderosmobile.android.plugins.bridge.GBridge")
local activity = bridge.getActivity()
Connection = Core.class()
 
function Connection:init()
	self.n_class = native.getClass("com.giderosmobile.android.Connection")
end
 
function Connection:isNetworkAvailable()
	return self.n_class.isNetworkAvailable(activity)
end
It works fine, but maybe there is a better way to check network connection? Because it works only for android.

Comments

  • edited October 2016 Accepted Answer
    I think you should send a GET request to http://google.com or apple.com (big guys on internet). If event.isError then we assume that this is no internet connection. Else if onComplete reach, we sure there is internet connection.
     
    _G.internet = false
    local function onComplete(event)
    	_G.internet = true
    	print("User have internet")
    end
     
    local function onError()
    	_G.internet = false
    	print("Error connecting to internet")
    end
     
    local loader = UrlLoader.new('<a href="http://google.com'" rel="nofollow">http://google.com'</a>)
     
    -- register to COMPLETE and ERROR events
    loader:addEventListener(Event.COMPLETE, onComplete)
    loader:addEventListener(Event.ERROR, onError)
    And because of onComplete and onError are async function so you should delay some time to ensure the result.
    Coming soon
  • jdbcjdbc Member
    edited October 2016
    I use an AbstractActivity in java Android code to check Internet connection in some of my games.
  • I think you should send a GET request to http://google.com or apple.com (big guys on internet). If event.isError then we assume that this is no internet connection. Else if onComplete reach, we sure there is internet connection.
     
    _G.internet = false
    local function onComplete(event)
    	_G.internet = true
    	print("User have internet")
    end
     
    local function onError()
    	_G.internet = false
    	print("Error connecting to internet")
    end
     
    local loader = UrlLoader.new('<a href="http://google.com'" rel="nofollow">http://google.com'</a>)
     
    -- register to COMPLETE and ERROR events
    loader:addEventListener(Event.COMPLETE, onComplete)
    loader:addEventListener(Event.ERROR, onError)
    And because of onComplete and onError are async function so you should delay some time to ensure the result.
    Is www.google.com available in all contries ?
  • @jeromegsq Nice catch! So maybe we should rely on apple.com? or giderosmobile.com :P

    Likes: jeromegsq

    Coming soon
    +1 -1 (+1 / -0 )Share on Facebook
  • simwhisimwhi Member
    edited October 2016
  • Apollo14Apollo14 Member
    edited June 2018
    Hey guys! Why does UrlLoader shows error first, but then completes loading? If image loads, what was the error then?
    My test code: https://pastebin.com/MjW8AHWV (because of image url, code isn't shown properly on forum)
    I'm getting in my console:
    let's check connection:
    internet connection error!
    internet is working!
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
Sign In or Register to comment.