时间:2020-11-13 08:41:22 | 栏目:Android代码 | 点击:次
在项目中,经常需要判断是否有网络连接。最近学习了如何判断软件是否联网,如果没有联网,弹出提示信息,连接网络。
效果:
(1)联网情况下:
(2)不联网情况下:
(3)点击“检测设置”:
源码下载:http://xiazai.jb51.net/201605/yuanma/MyApplication(jb51.net).rar
判断我们的软件是否联网,看代码吧:
/** * 检测网络连接 * * @param con * @return */ public boolean isNetworkAvailable(Context con) { ConnectivityManager cm = (ConnectivityManager) con .getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) return false; NetworkInfo netinfo = cm.getActiveNetworkInfo(); if (netinfo == null) { return false; } if (netinfo.isConnected()) { return true; } return false; }
如果没有联网,弹出提示框,提示设置网络连接:
/** * 提示设置网络连接对话框 * * @param context */ public void showNetDialog(final Context context) { mMaterialDialog = new MaterialDialog(context) .setMessage("世界上最遥远的距离就是没网") .setPositiveButton("检查设置", new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = null; try { @SuppressWarnings("deprecation") String sdkVersion = android.os.Build.VERSION.SDK; if (Integer.valueOf(sdkVersion) > 10) { intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); } else { intent = new Intent(); ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); intent.setComponent(comp); intent.setAction("android.intent.action.VIEW"); } context.startActivity(intent); } catch (Exception e) { e.printStackTrace(); } mMaterialDialog.dismiss(); } }).setNegativeButton("取消", new View.OnClickListener() { @Override public void onClick(View v) { mMaterialDialog.dismiss(); } }); mMaterialDialog.show(); }
怎么做判断处理:
/** * 判断是否联网 */ private void checkNet() { if(!mCheckNetWork.isNetworkAvailable(getApplication())){ mCheckNetWork.showNetDialog(MainActivity.this); }else { Toast.makeText(MainActivity.this, "有网络,哈哈",Toast.LENGTH_SHORT).show(); } }
具体代码如下:
MainActivity.java
package com.bzu.gxs.chectnetwork; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener{ private CheckNetWork mCheckNetWork =new CheckNetWork(); private Button btn_check; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } /** * 点击事件 * * @param view */ @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_check: checkNet(); } } /** * 判断是否联网 */ private void checkNet() { if(!mCheckNetWork.isNetworkAvailable(getApplication())){ mCheckNetWork.showNetDialog(MainActivity.this); }else { Toast.makeText(MainActivity.this, "有网络,哈哈",Toast.LENGTH_SHORT).show(); } } /** * 初始化 */ private void init() { btn_check = (Button) findViewById(R.id.btn_check); btn_check.setOnClickListener(this); } }
CheckNetWork.java
package com.bzu.gxs.chectnetwork; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.media.browse.MediaBrowser; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; import android.view.View; import me.drakeet.materialdialog.MaterialDialog; /** * 网络检测 * Created by GXS on 2016/5/13. */ public class CheckNetWork { private MaterialDialog mMaterialDialog; /** * 检测网络连接 * * @param con * @return */ public boolean isNetworkAvailable(Context con) { ConnectivityManager cm = (ConnectivityManager) con .getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) return false; NetworkInfo netinfo = cm.getActiveNetworkInfo(); if (netinfo == null) { return false; } if (netinfo.isConnected()) { return true; } return false; } /** * 提示设置网络连接对话框 * * @param context */ public void showNetDialog(final Context context) { mMaterialDialog = new MaterialDialog(context) .setMessage("世界上最遥远的距离就是没网") .setPositiveButton("检查设置", new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = null; try { @SuppressWarnings("deprecation") String sdkVersion = android.os.Build.VERSION.SDK; if (Integer.valueOf(sdkVersion) > 10) { intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); } else { intent = new Intent(); ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); intent.setComponent(comp); intent.setAction("android.intent.action.VIEW"); } context.startActivity(intent); } catch (Exception e) { e.printStackTrace(); } mMaterialDialog.dismiss(); } }).setNegativeButton("取消", new View.OnClickListener() { @Override public void onClick(View v) { mMaterialDialog.dismiss(); } }); mMaterialDialog.show(); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.bzu.gxs.chectnetwork.MainActivity"> <Button android:id="@+id/btn_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试"/> </RelativeLayout>
注意:需要在清单文件AndroidManifest.xml中加入
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>