继续在Android应用中检查互联网连接 [检查互联网是否可用]

huangapple go评论90阅读模式
英文:

Continues Internet Connection Checking in Android App [Checking Availability of Internet]

问题

在查看问题标题并采取任何行动之前,请仔细阅读描述。我昨天和今天整天整夜都在尝试,但我找不到任何可接受的解决方案。有些从安卓中废弃,有些不起作用,有些只是检查互联网是否连接。但这不是我想要的。

在这方面,我已经应用了许多Stack Overflow上的答案,但没有任何一个完美地工作。我要求的功能是,假设MathActivity.javaactivity_math.xml是在从mainActivity.java点击一个按钮后加载的页面。当点击按钮后启动MathActivity.java时,它首先可以通过ping方法检查活动互联网连接。尽管它不能正常工作,

public boolean checkIntCON() {
    try {
        Process ipProcess = Runtime.getRuntime().exec("/system/bin/ping -c 1 8.8.8.8");
        return (ipProcess.waitFor() == 0);
    }
    catch (IOException e)          { e.printStackTrace(); }
    catch (InterruptedException e) { e.printStackTrace(); }
    return false;
}

但它只在我进入时检查一次(我做过这个工作,但我不想要那个)。比如用户正在activity_math.xml中阅读PDF,当用户在阅读时断开了互联网连接,我也能听到这个功能,当用户重新打开互联网连接但无法访问互联网时,我不能监听到这个。

我想要得到一个最新且最大程度支持的解决方案,以连续检查互联网连接。比如用户在阅读PDF,五分钟后我想要检查用户是否有活动的互联网连接。连接必须是可传输的互联网连接。我已经找到了AsyncTask的解决方案,但它已被废弃。主要的焦点是,我想要在一定时间间隔后检查用户的活动互联网连接。

这里有一张图片来进行更详细的解释,
继续在Android应用中检查互联网连接 [检查互联网是否可用]

是否有任何解决方案?如果有,请尝试详细说明代码,我只想要将其复制粘贴到项目中。我已经访问了Stack Overflow上的所有内容,我再次提到这一点。

英文:

Before answer or taking any action just by seeing the question title please read description carefully, I am trying to do this yesterday and today a full day and night but I couldn't find any acceptable solution. Some are deprecated from android some are not working, some are just checking internet is just connected or not. But that's not what I am asking for.

I have applied many Stack Overflow answers in this regard but nothing is working perfectly. The functionality I am asking for is,
say MathActivity.java and activity_math.xml is a page loaded after clicking a button from mainActivity.java. When MathActivity.java is starting after clicking the button its primarily can check active internet connection via the ping method. Though its not working properly,

public boolean checkIntCON() {
        try {
            Process ipProcess = Runtime.getRuntime().exec("/system/bin/ping -c 1 8.8.8.8");
            return (ipProcess.waitFor() == 0);
        }
        catch (IOException e)          { e.printStackTrace(); }
        catch (InterruptedException e) { e.printStackTrace(); }
        return false;
    }

But its check just one time when I am entering [I did that work but I don't want that], say user is reading a pdf in activity_math.xml and when user reading this user turned off internet connection, I am also able to listen that functionality when user turn off connection this would be ok but if user again turn on data with internet unavailability then I can't listen this .

I want to get recent and maximum device supported solution to check in continues internet connection. say user is reading a pdf, after five minutes I want to check if the user have an active internet connection or not. Connection should be must transferrable internet connection. I have found AsyncTask solution but its deprecated. The main focus is I want to check user active internet connection after a certain interval.

Here is an image to explain more,
继续在Android应用中检查互联网连接 [检查互联网是否可用]

Is there any solution? If any please try to give me in detail code that i just want to copy and paste in project. I have visited all in stack overflow again I am mentioning.

答案1

得分: 2

将上下文传递并调用该函数,我认为这就是你要寻找的内容。

 public static boolean isConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
            Log.d("VIP", "isConnected: " + capabilities.getTransportInfo() + " 下行速率 " + capabilities.getLinkDownstreamBandwidthKbps() +
                    " 上行速率 " + capabilities.getLinkUpstreamBandwidthKbps() + " 蜂窝网络 " + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
                    + " WiFi " + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) + " 以太网 " + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
            return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
        } else {
            NetworkInfo networkInfo = Objects.requireNonNull(connectivityManager).getActiveNetworkInfo();
            boolean b = networkInfo != null && networkInfo.isConnected() && networkInfo.getDetailedState() != NetworkInfo.DetailedState.VERIFYING_POOR_LINK;
            Log.d("VIP", "isConnected: else is connected " + b);
            return b;
        }
    }
英文:

Pass context and call the function, I think this is what you are looking for.

 public static boolean isConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
            Log.d("VIP", "isConnected: " + capabilities.getTransportInfo() + " down stream " + capabilities.getLinkDownstreamBandwidthKbps() +
                    " up stream " + capabilities.getLinkUpstreamBandwidthKbps() + " Cellular " + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
                    + " WiFi " + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) + " Ethernet " + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
            return capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);
        } else {
            NetworkInfo networkInfo = Objects.requireNonNull(connectivityManager).getActiveNetworkInfo();
            boolean b = networkInfo != null && networkInfo.isConnected() && networkInfo.getDetailedState() != NetworkInfo.DetailedState.VERIFYING_POOR_LINK;
            Log.d("VIP", "isConnected: else is connected " + b);
            return b;
        }
    }

huangapple
  • 本文由 发表于 2020年10月9日 17:48:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64277641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定