栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

如何判断软件程序是否联网 联网状态提示信息Android实现

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何判断软件程序是否联网 联网状态提示信息Android实现

在项目中,经常需要判断是否有网络连接。最近学习了如何判断软件是否联网,如果没有联网,弹出提示信息,连接网络。

效果:

(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中加入


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/162473.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号