android - 我如何检查android中的网络连接?

标签 android wifi gprs

我开发了一个应用程序,因为我必须检查网络是否已连接我使用以下代码但它不起作用。谁能解决我的问题。我的代码是

public class AbcActivity extends Activity {
    Button b;
    private static final String tag = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                netCheckin();
            }
        });
    }
        private void netCheckin() {

            try {

                ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                nInfo.getActiveNetworkInfo().isConnectedOrConnecting();

                Log.d(tag, "Net avail:"
                        + nInfo.getActiveNetworkInfo().isConnectedOrConnecting());

                ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                   Toast.makeText(getApplicationContext(), "net on", Toast.LENGTH_LONG).show();
                    Log.d(tag, "Network available:true");

                } else {
                     Toast.makeText(getApplicationContext(), "net OFF", Toast.LENGTH_LONG).show();
                    Log.d(tag, "Network available:false");

                }

            } catch (Exception e) {
               Log.e("error",""+e);
            }
        }

    }

我已经给了 android:name="android.permission.ACCESS_NETWORK_STATE"/> 在我的 list 文件中 在上面的代码中,我的问题是当 net 启用或禁用时它给出与“net on”(toast 消息)相同的输出。

最佳答案

在 onCreate 方法的顶部,使用

if(!checkInternetConnection()){
            // network connnectivity error , show some dialog here and exit from the app 

        }

在哪里

private boolean checkInternetConnection() {

            ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            // ARE WE CONNECTED TO THE NET
            if (conMgr.getActiveNetworkInfo() != null
                    && conMgr.getActiveNetworkInfo().isAvailable()
                    && conMgr.getActiveNetworkInfo().isConnected()) {

                return true;

            } else {
                Log.v("", "Internet Connection Not Present");
                return false;
            }
        }

希望对你有帮助。

关于android - 我如何检查android中的网络连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9000269/

相关文章:

ios - 当我的 iPhone 手机中的 wifi 连接时打开一些链接

c# - 类型 native Wifi.Wlan + WlanReasonCode 无法编码错误

android - 如何强制使用 GPRS 连接而不是 3G 或 Wifi?

http - sim800 at command post 数据到服务器

android - 在文件资源管理器中仅探索数据/数据/

android - Android “Installs targeted by rollout” 是指能够看到更新或能够安装更新的用户百分比吗?

android - Android 上的 Qt。添加 AndroidManifest.xml 应用后不启动

java - 我的 json 响应中的全屏 Activity 中未显示图像..?滑动的同时也

android - 使用 adb 连接 WIFI

javascript - 使用 GPRS 将 Arduino 连接到 Firebase