在android中检查互联网连接(不是网络连接) [英] Check internet connection in android (not network connection)

查看:89
本文介绍了在android中检查互联网连接(不是网络连接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时检查android中的互联网连接时遇到问题.我使用几种不同的方法来检查Internet连接,但我不知道哪种方法更好.因为他们每个人都有一些问题.

I have a problem with checking internet connection in android at runtime. I use some different methods to check internet connection but i don't know which one is better . because each of them have some problems .

方法1 通过 ping Google 来检查Internet连接:

Method 1 check internet connection by pinging Google :

Runtime runtime = Runtime.getRuntime();
try {
       Process mIpAddressProcess = runtime.exec("/system/bin/ping -c 1   8.8.8.8");
       int mExitValue = mIpAddressProcess.waitFor();
       return mExitValue == 0;
} catch (InterruptedException | IOException ignore) {
            ignore.printStackTrace();
}

方法2 通过 ConnectivityManager 来检查Internet连接:

Method 2 check internet connection by ConnectivityManager :

public boolean checkNetwork() {

    ConnectivityManager internetManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = internetManager.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected() && networkInfo.isAvailable() && networkInfo.isConnectedOrConnecting());

}

我在异步任务中使用了方法1,但是有时它无法正常工作,从而降低了应用的运行速度,方法2(ConnectivityManager)不会检查互联网连接,它只会检查网络连接!

I use method 1 inside an async task, but it doesn't work correctly sometimes and slow down the app, and Method 2 (ConnectivityManager) doesn't check internet connection , it checks only network connection !

推荐答案

我每次都在使用广播检查连接.创建一个用于连接信息的类.

I'm using broadcast to check the connection every time. Create a class for connection info.

import android.content.Context;
import android.content.ContextWrapper;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


public class ConnectivityStatus extends ContextWrapper{

    public ConnectivityStatus(Context base) {
        super(base);
    }

    public static boolean isConnected(Context context){

        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo connection = manager.getActiveNetworkInfo();
        if (connection != null && connection.isConnectedOrConnecting()){
            return true;
        }
        return false;
    }
}

将代码应用于活动":

 private BroadcastReceiver receiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
        if(!ConnectivityStatus.isConnected(getContext())){
            // no connection
        }else {
            // connected
        }
    }
 };

使用活动的 onCreate()方法注册广播:

Register broadcast in your activity's onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    your_activity_context.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    ..
    ...
    ....
  }

别忘了在活动周期内注销/注册:

Don't forget to unregistered/register on Activity cycle:

@Override
protected void onResume() {
    super.onResume();
    your_activity_context.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

}

@Override
protected void onPause() {
    super.onPause();
    your_activity_context.unregisterReceiver(receiver);

}

这篇关于在android中检查互联网连接(不是网络连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆