zoukankan      html  css  js  c++  java
  • Android 检測网络是否连接

    权限:

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    代码例如以下:

    package com.example.nettest;

    import android.net.ConnectivityManager;
    import android.net.NetworkInfo.State;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.view.Menu;
    import android.widget.TextView;
    import android.widget.Toast;


    public class MainActivity extends Activity {


    private ConnectivityManager manager;
    private TextView tv;
    StringBuffer sb = new StringBuffer(256);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    tv = (TextView) findViewById(R.id.textView1);
    checkNetworkState();
    }


    /**
    * 检測网络是否连接

    * @return
    */
    private boolean checkNetworkState() {
    boolean flag = false;
    // 得到网络连接信息
    manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    // 去进行推断网络是否连接
    if (manager.getActiveNetworkInfo() != null) {
    flag = manager.getActiveNetworkInfo().isAvailable();
    }
    if (!flag) {
    setNetwork();
    } else {
    isNetworkAvailable();
    }
    tv.setText(sb.toString());
    return flag;
    }


    /**
    * 网络未连接时。调用设置方法
    */
    private void setNetwork() {
    Toast.makeText(this, "wifi is closed!", Toast.LENGTH_SHORT).show();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.drawable.ic_launcher);
    builder.setTitle("网络提示信息");
    builder.setMessage("网络不可用。假设继续,请先设置网络!

    ");
    builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Intent intent = null;
    /**
    * 推断手机系统的版本号!

    假设API大于10 就是3.0+ 由于3.0以上的版本号的设置和3.0下面的设置不一样。调用的方法不同
    */
    if (android.os.Build.VERSION.SDK_INT > 10) {
    intent = new Intent(
    android.provider.Settings.ACTION_SETTINGS);
    } else {
    intent = new Intent();
    ComponentName component = new ComponentName(
    "com.android.settings",
    "com.android.settings.WirelessSettings");
    intent.setComponent(component);
    intent.setAction("android.intent.action.VIEW");
    }
    startActivity(intent);
    }
    });


    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    }
    });
    builder.create();
    builder.show();
    }


    /**
    * 网络已经连接,然后去推断是wifi连接还是GPRS连接 设置一些自己的逻辑调用
    */
    private void isNetworkAvailable() {


    State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
    .getState();
    State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
    .getState();
    if (gprs == State.CONNECTED || gprs == State.CONNECTING) {
    Toast.makeText(this, "gprs is open! ", Toast.LENGTH_SHORT).show();
    sb.append(" gprs is open! ");
    } else {
    sb.append(" gprs is closed! ");
    }


    // 推断为wifi状态下才载入广告,假设是GPRS手机网络则不载入。
    if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
    Toast.makeText(this, "wifi is open! ", Toast.LENGTH_SHORT).show();
    loadAdmob();
    sb.append(" wifi is open! ");
    } else {
    sb.append(" wifi is closed! ");
    }


    }


    /**
    * 在wifi状态下 载入admob广告
    */
    private void loadAdmob() {
    Toast.makeText(getApplicationContext(), "ad is loding..", 1).show();
    sb.append(" ad is loding...");
    }
    }

  • 相关阅读:
    install jqdatasdk
    分布式唯一ID自增(雪花算法)
    JVM内存布局及GC知识
    double涉及大数据的时候会变成科学计数法
    IDEA中的JUNIT测试
    spring boot 整合 swagger2
    springboot 报错nested exception is java.lang.IllegalStateException: Failed to check the status of the service xxxService No provider available for the service
    解决jar包依赖冲突(idea)
    IDEA中springboot的热部署
    Exception in thread "main" java.lang.AbstractMethodError: org.springframework.boot.context.config.ConfigFileApplicationListener.supportsSourceType(Ljava/lang/Class;)Z
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5094611.html
Copyright © 2011-2022 走看看