在项目中,经常需要判断是否有网络连接。最近学习了如何判断软件是否联网,如果没有联网,弹出提示信息,连接网络。
效果:
(1)联网情况下:
(2)不联网情况下:
(3)点击“检测设置”:
源码下载:http://xiazai.jb51.net/201605/yuanma/MyApplication(jb51.net).rar
判断我们的软件是否联网,看代码吧:
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;
}
如果没有联网,弹出提示框,提示设置网络连接:
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();
}
@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;
public class CheckNetWork {
private MaterialDialog mMaterialDialog;
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;
}
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
注意:需要在清单文件AndroidManifest.xml中加入
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



