【问题标题】:How to check if my app is using WIFI or 3G service如何检查我的应用程序使用的是 WIFI 还是 3G 服务
【发布时间】:2014-03-11 09:53:23
【问题描述】:

我正在开发一个消息应用程序,是否有任何 API 来识别 android 中的服务提供商名称,还有人可以帮助我如何以编程方式检查应用程序是使用 WIFI 连接还是 3G 服务?请帮帮我。

【问题讨论】:

标签: java android


【解决方案1】:

通过这个 sn-p 代码块,您可以检查设备中的所有类型网络:

public boolean isNetworkAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

然后通过以下方式测试您的网络:

if (isNetworkAvailable()) {
                    //Network availabe...
                } else {
                    Toast alrtMsg = Toast.makeText(this, "No network connection available !!!", Toast.LENGTH_LONG);
                    alrtMsg.setGravity(Gravity.CENTER, 0, 0);
                    alrtMsg.show();
                }

【讨论】:

    【解决方案2】:

    这是一个非常好的类,可以获取当前的连接类型并通过连接更新的侦听器进行提醒:

    package com.android.aft.AFCoreTools;
    
    import java.util.ArrayList;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    
    
    public class AFNetworkMonitoring {
    
        public enum NetworkMode {
            NotConnected,
            ConnectedToWifi,
            ConnectedTo3G,
        };
    
        public interface NetworkMonitoringInterface {
            public void onNetworkUpdate(NetworkMode mode);
        }
    
        // Network connection
        public NetworkMode mode = NetworkMode.NotConnected;
    
        // Connection listener
        private BroadcastReceiver mConnReceiver;
    
        // Listeners
        private ArrayList<NetworkMonitoringInterface> mListeners;
    
        // Context
        private Context mContext;
    
        public AFNetworkMonitoring(Context ctx) {
            this(ctx, null);
        }
    
        public AFNetworkMonitoring(Context ctx, NetworkMonitoringInterface listener) {
            mContext = ctx;
            addListener(listener);
    
            // Create network state update
            mConnReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    updateNetworkConnectionStatus();
                }
            };
            mContext.registerReceiver(mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
    
        protected void updateNetworkConnectionStatus() {
            ConnectivityManager conMgr = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = conMgr.getActiveNetworkInfo();
            if (info == null || !info.isConnected() || !info.isAvailable()) {
                DebugTools.d("Lost connection detected");
                mode = NetworkMode.NotConnected;
                notifyListener();
                return;
            }
    
            NetworkInfo infoWifi = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (infoWifi.isConnected() || infoWifi.isAvailable()) {
                DebugTools.d("On connection wifi detected");
                mode = NetworkMode.ConnectedToWifi;
                notifyListener();
                return;
            }
    
            DebugTools.d("On connection 3G detected");
            mode = NetworkMode.ConnectedTo3G;
            notifyListener();
        }
    
        protected void notifyListener() {
            if (mListeners == null)
                return ;
    
            new AsyncTaskWrapper<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    // Nothing to do
                    return null;
                }
                @Override
                protected void onPostExecute(Void result) {
                    for (NetworkMonitoringInterface l: mListeners)
                        l.onNetworkUpdate(mode);
                };
            }.executeParallel();
        }
    
        public void addListener(NetworkMonitoringInterface listener) {
            if (listener == null)
                return ;
    
            if (mListeners == null)
                mListeners = new ArrayList<AFNetworkMonitoring.NetworkMonitoringInterface>();
    
            mListeners.add(listener);
        }
    
        public void removeListener(NetworkMonitoringInterface listener) {
            if (mListeners == null)
                return ;
            mListeners.remove(listener);
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      LocationManager 服务 = (LocationManager)getSystemService(getActivity().LOCATION_SERVICE);

      布尔 GPS_PROVIDER = service.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean NETWORK_PROVIDER = service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2012-04-09
        • 2013-06-13
        • 1970-01-01
        • 2016-06-26
        • 2016-12-23
        • 1970-01-01
        相关资源
        最近更新 更多