我应该检查Android中每项活动的连接吗?

时间:2015-05-03 19:07:34

标签: android connectivity

所以我刚刚实现了检查连接方法,但是我应该为每个活动执行此操作吗?是否有更简单的方法来持续检查连接?

public static boolean isNetworkAvailable(Context context) {

    ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    boolean isConnected = activeNetwork != null &&
            activeNetwork.isConnectedOrConnecting();

    return isConnected;
}

5 个答案:

答案 0 :(得分:3)

首先创建一个ConnectivityChangeReceiver,如下所示:

public class ConnectivityChangeReceiver extends BroadcastReceiver {

    private OnConnectivityChangedListener listener;

    public ConnectivityChangeReceiver(OnConnectivityChangedListener listener) {
        this.listener = listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) 
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();  
        listener.onConnectivityChanged(isConnected);
    }

    public interface OnConnectivityChangedListener {
        void onConnectivityChanged(boolean isConnected);
    }
}

然后创建一个BaseActivity,扩展您的所有活动:

public class BaseActivity extends Activity implements OnConnectivityChangedListener {

    private ConnectivityChangeReceiver connectivityChangeReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        connectivityChangeReceiver = new ConnectivityChangeReceiver(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(connectivityChangeReceiver, filter);
    }

    @Override
    public void onConnectivityChanged(boolean isConnected) {
        // TODO handle connectivity change
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(connectivityChangeReceiver);
    }
}

(如果您不想要BaseActivity,则可以在所有活动中实施OnConnectivityChangedListener。但在这种情况下,您必须在所有Activity中注册接收器。)

答案 1 :(得分:1)

您应该在每次上网请求之前检查!用户可以在任何给定时间终止连接...即使您的活动正在运行。

我建议您创建一个Utils类并将此方法放入其中并使其成为公共静态,以便您可以在Application内的任何位置访问它。

答案 2 :(得分:1)

您可以查看:这是将持续检查连接的服务。这将是正确的方法

`private void installListener(){

    if (broadcastReceiver == null) {

        broadcastReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                Bundle extras = intent.getExtras();

                NetworkInfo info = (NetworkInfo) extras
                        .getParcelable("networkInfo");

                State state = info.getState();
                Log.d("InternalBroadcastReceiver", info.toString() + " "
                        + state.toString());

                if (state == State.CONNECTED) {

                    onNetworkUp();

                } else {

                    onNetworkDown();

                }

            }
        };

        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(broadcastReceiver, intentFilter);
    }
}`

点击此链接:Android service to check internet connectivity?

答案 3 :(得分:1)

创建某种util类,例如ConnectionUtils

向该类添加public static方法,并在参数中添加Context对象。例如:

public static boolean isNetworkAvailable(Context context) {

    ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

    return isConnected;
}

然后在Activity中致电ConnectionUtils.isNetworkAvailable(this),了解您是否已连接。

创建一些使用此方法扩展的基础Activity甚至会更好。

public abstract class BaseActivity extends Activity {

    protected boolean isConnected() {
         return ConnectionUtils.isNetworkAvailable(this);
    }
}

然后让所有Activities延伸BaseActivity代替Activity

答案 4 :(得分:0)

我说最好在每次请求之前检查。可能会发生连接中断之间的连接。所以早些时候依靠支票是不明智的