Android 开发必备Android开发编程

Android 实现实时监听网络状态(广播适配7.0以上)

2019-11-01  本文已影响0人  总会颠沛流离

一:听说你还在用工具类来判断网络状态?垃圾

这样做会有什么问题呢?
首先最直观的,不够优雅,代码判断量太多,如果你的操作是需要频繁的监听网络状态,那么过多的if/ else肯定会让后面维护变的眼花缭乱。
以上方法只能在网络操作之前判断网络状态,若用户在网络正常情况下发起操作而中间改变网络,比如下载中途突然丢失网络,则此时无法做出相应的控制。
无法只针对某种网络类型进行监听,比如只想监听用户切换到 WiFi 网络时做出响应。
程序多处需要进行网络监听处理时,不能同时接收网络变化,必须逐个地方手动处理。

二:它的原理是什么

image.png

我们是通过注册广播来实现绑定网络变更的监听,在Android 7.0 以后,Google 基于性能和安全原因对广播进行了很多限制,比如监听网络变更的广播 android.net.conn.CONNECTIVITY_CHANGE 使用静态注册的方式则无法生效,而动态注册的方式可以生效但毕竟不是最优解。

第一步:废话少说直接上效果图

![ 超级截屏_20191101_134502.png 超级截屏_20191101_134434.png

第二步:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.networkmonitordemo">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:name=".App"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".utils.NetBroadcastReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
</application>

</manifest>

第三步:BaseActivity

package com.example.networkmonitordemo;

import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

  import androidx.annotation.Nullable;
  import androidx.appcompat.app.AppCompatActivity;

  import com.example.networkmonitordemo.utils.NetBroadcastReceiver;
  import com.example.networkmonitordemo.utils.NetUtil;
  import com.example.networkmonitordemo.utils.T;
  import com.example.networkmonitordemo.utils.dialog.MyAlertDialog;
  /**
 * @Author: 
 * @Date: 2019/11/1 14:03
 * @Description: 2531295581
 */
public abstract class BaseActivity  extends AppCompatActivity implements NetBroadcastReceiver.NetChangeListener {
public static NetBroadcastReceiver.NetChangeListener listener;
private MyAlertDialog alertDialog=null;
private NetBroadcastReceiver netBroadcastReceiver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    //全部禁止横屏
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    super.onCreate(savedInstanceState);
    setContentView(initLayout());
    listener = this;
    //Android 7.0以上需要动态注册
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        //实例化IntentFilter对象
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netBroadcastReceiver = new NetBroadcastReceiver();
        //注册广播接收
        registerReceiver(netBroadcastReceiver, filter);
    }
    checkNet();
    initView();
    initData();
}

  /**
 * 网络类型
 */
private int netType;

protected abstract int initLayout();

protected abstract void initView();

protected abstract void initData();
/**
 * 网络变化之后的类型
 */
@Override
public void onChangeListener(int status) {
    // TODO Auto-generated method stub
    this.netType = status;
    Log.i("netType", "netType:" + status);
    if (!isNetConnect()) {
        showNetDialog();
        T.showShort("网络异常,请检查网络,哈哈");
    } else {
        hideNetDialog();
        T.showShort("网络恢复正常");
    }
}

/**
 * 隐藏设置网络框
 */
private void hideNetDialog() {
    if (alertDialog != null) {
        alertDialog.dismiss();
    }
    alertDialog = null;
}
    /**
 * 初始化时判断有没有网络
 */
public boolean checkNet() {
    this.netType = NetUtil.getNetWorkState(BaseActivity.this);
    if (!isNetConnect()) {
        //网络异常,请检查网络
        showNetDialog();
        T.showShort("网络异常,请检查网络,哈哈");
    }
    return isNetConnect();
}
/**
 * 判断有无网络 。
 *
 * @return true 有网, false 没有网络.
 */
public boolean isNetConnect() {
    if (netType == 1) {
        return true;
    } else if (netType == 0) {
        return true;
    } else if (netType == -1) {
        return false;
    }
    return false;
}

/**
 * 弹出设置网络框
 */
private void showNetDialog() {
    if (alertDialog == null) {
        alertDialog = new MyAlertDialog(this).builder().setTitle("网络异常")
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                }).setPositiveButton("设置", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                        startActivity(intent);
                    }
                }).setCancelable(false);
    }
    alertDialog.show();
    showMsg("网络异常,请检查网络");
}
public void showMsg(String msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}

第四步:MainActivity

 package com.example.networkmonitordemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
/**
 * @Author: 
 * @Date: 2019/11/1 14:03
 * @Description: 2531295581
 */
public class MainActivity extends BaseActivity {


@Override
protected int initLayout() {
    return R.layout.activity_main;
}

@Override
protected void initView() {

}

@Override
protected void initData() {

}
}

第五步:MyAlertDialog

  /**
   * @Author: 
   * @Date: 2019/11/1 14:03
   * @Description: 2531295581
   */

    public class MyAlertDialog {
private Context context;
private Dialog dialog;
private LinearLayout lLayout_bg;
private TextView txt_title;
private TextView txt_msg;
private EditText edittxt_result;
private LinearLayout dialog_Group;
private ImageView dialog_marBottom;
private Button btn_neg;
private Button btn_pos;
private ImageView img_line;
private Display display;
private boolean showTitle = false;
private boolean showMsg = false;
private boolean showEditText = false;
private boolean showLayout = false;
private boolean showPosBtn = false;
private boolean showNegBtn = false;

public MyAlertDialog(Context context) {
    this.context = context;
    WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    display = windowManager.getDefaultDisplay();
}

public MyAlertDialog builder() {
    // 获取Dialog布局
    View view = LayoutInflater.from(context).inflate(R.layout.toast_view_alertdialog, null);

    // 获取自定义Dialog布局中的控件
    lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
    txt_title = (TextView) view.findViewById(R.id.txt_title);
    txt_title.setVisibility(View.GONE);
    txt_msg = (TextView) view.findViewById(R.id.txt_msg);
    txt_msg.setVisibility(View.GONE);
    edittxt_result = (EditText) view.findViewById(R.id.edittxt_result);
    edittxt_result.setVisibility(View.GONE);
    dialog_Group = (LinearLayout) view.findViewById(R.id.dialog_Group);
    dialog_Group.setVisibility(View.GONE);
    dialog_marBottom = (ImageView) view.findViewById(R.id.dialog_marBottom);
    btn_neg = (Button) view.findViewById(R.id.btn_neg);
    btn_neg.setVisibility(View.GONE);
    btn_pos = (Button) view.findViewById(R.id.btn_pos);
    btn_pos.setVisibility(View.GONE);
    img_line = (ImageView) view.findViewById(R.id.img_line);
    img_line.setVisibility(View.GONE);

    // 定义Dialog布局和参数
    dialog = new Dialog(context, R.style.AlertDialogStyle);
    dialog.setContentView(view);

    // 调整dialog背景大小
    lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth() * 0.85),
            LayoutParams.WRAP_CONTENT));

    return this;
}

public MyAlertDialog setTitle(String title) {
    showTitle = true;
    if ("".equals(title)) {
        txt_title.setText("标题");
    } else {
        txt_title.setText(title);
    }
    return this;
}

public MyAlertDialog setEditText(String msg) {
    showEditText = true;
    if ("".equals(msg)) {
        edittxt_result.setHint("内容");
    } else {
        edittxt_result.setHint(msg);
    }
    return this;
}

public MyAlertDialog setEditType(int editType) {
    edittxt_result.setInputType(editType);
    return this;
}

public String getResult() {
    return edittxt_result.getText().toString();
}

public MyAlertDialog setMsg(String msg) {
    showMsg = true;
    if ("".equals(msg)) {
        txt_msg.setText("内容");
    } else {
        txt_msg.setText(msg);
    }
    return this;
}

public MyAlertDialog setView(View view) {
    showLayout = true;
    if (view == null) {
        showLayout = false;
    } else
        dialog_Group.addView(view, android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
    return this;
}

public MyAlertDialog setCancelable(boolean cancel) {
    dialog.setCancelable(cancel);
    return this;
}

public MyAlertDialog setCanceledOnTouchOutside(boolean cancel) {
    dialog.setCanceledOnTouchOutside(cancel);
    return this;
}

public MyAlertDialog setPositiveButton(String text, final OnClickListener listener) {
    showPosBtn = true;
    if ("".equals(text)) {
        btn_pos.setText("确定");
    } else {
        btn_pos.setText(text);
    }
    btn_pos.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onClick(v);
            dialog.dismiss();
        }
    });
    return this;
}

public MyAlertDialog setNegativeButton(String text, final OnClickListener listener) {
    showNegBtn = true;
    if ("".equals(text)) {
        btn_neg.setText("取消");
    } else {
        btn_neg.setText(text);
    }
    btn_neg.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onClick(v);
            dialog.dismiss();
        }
    });
    return this;
}

private void setLayout() {
    if (!showTitle && !showMsg) {
        txt_title.setText("提示");
        txt_title.setVisibility(View.VISIBLE);
    }

    if (showTitle) {
        txt_title.setVisibility(View.VISIBLE);
    }

    if (showEditText) {
        edittxt_result.setVisibility(View.VISIBLE);
    }

    if (showMsg) {
        txt_msg.setVisibility(View.VISIBLE);
    }

    if (showLayout) {
        dialog_Group.setVisibility(View.VISIBLE);
        dialog_marBottom.setVisibility(View.GONE);
    }

    if (!showPosBtn && !showNegBtn) {
        btn_pos.setText("确定");
        btn_pos.setVisibility(View.VISIBLE);
        btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
        btn_pos.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    if (showPosBtn && showNegBtn) {
        btn_pos.setVisibility(View.VISIBLE);
        btn_pos.setBackgroundResource(R.drawable.alertdialog_right_selector);
        btn_neg.setVisibility(View.VISIBLE);
        btn_neg.setBackgroundResource(R.drawable.alertdialog_left_selector);
        img_line.setVisibility(View.VISIBLE);
    }

    if (showPosBtn && !showNegBtn) {
        btn_pos.setVisibility(View.VISIBLE);
        btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
    }

    if (!showPosBtn && showNegBtn) {
        btn_neg.setVisibility(View.VISIBLE);
        btn_neg.setBackgroundResource(R.drawable.alertdialog_single_selector);
    }
}

public void show() {
    setLayout();
    dialog.show();
}

public void dismiss() {
    if (dialog != null && dialog.isShowing()) {
        dialog.dismiss();
    }
    dialog = null;
}
}

第六步:NetBroadcastReceiver

  public class NetBroadcastReceiver extends BroadcastReceiver {

public NetChangeListener listener = BaseActivity.listener;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    // 如果相等的话就说明网络状态发生了变化
    Log.i("NetBroadcastReceiver", "NetBroadcastReceiver changed");
    if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        int netWorkState = NetUtil.getNetWorkState(context);
        // 当网络发生变化,判断当前网络状态,并通过NetEvent回调当前网络状态
        if (listener != null) {
            listener.onChangeListener(netWorkState);
        }
    }
}

// 自定义接口
public interface NetChangeListener {
    void onChangeListener(int status);
}

}

第七步:NetUtil

 public class NetUtil {
/**
 * 没有网络
 */
private static final int NETWORK_NONE = -1;
/**
 * 移动网络
 */
private static final int NETWORK_MOBILE = 0;
/**
 * 无线网络
 */
private static final int NETWORK_WIFI = 1;

public static int getNetWorkState(Context context) {
    //得到连接管理器对象
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();
    //如果网络连接,判断该网络类型
    if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
        if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
            return NETWORK_WIFI;//wifi
        } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
            return NETWORK_MOBILE;//mobile
        }
    } else {
        //网络异常
        return NETWORK_NONE;
    }
    return NETWORK_NONE;
}
}

第八步:Toast统一管理类

  /**
 * Toast统一管理类
 */
  public class T {
// Toast
private static Toast toast;

private T() {

}

public static void init(Context context) {
    toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
}

/**
 * 短时间显示Toast
 * 
 * @param message
 */
public static void showShort(String message, Object... args) {
    show(Toast.LENGTH_SHORT, message, args);
}

/**
 * 短时间显示Toast
 * 
 * @param message
 */
public static void showShort(int message) {
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setText(message);
    toast.show();
}

/**
 * 长时间显示Toast
 * 
 * @param message
 */
public static void showLong(String message, Object... args) {
    show(Toast.LENGTH_LONG, message, args);
}

/**
 * 长时间显示Toast
 * 
 * @param message
 */
public static void showLong(int message) {
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setText(message);
    toast.show();
}

/**
 * @param duration
 * @param message
 * @param args
 */
public static void show(int duration, String message, Object... args) {
    toast.setDuration(duration);
    if (args.length > 0) {
        message = String.format(message, args);
    }
    toast.setText(message);
    toast.show();
}

/**
 * 自定义显示Toast时间
 * 
 * @param message
 * @param duration
 */
public static void show(int message, int duration) {
    toast.setDuration(duration);
    toast.setText(message);
    toast.show();
}

/** Hide the toast, if any. */
public static void hideToast() {
    if (null != toast) {
        toast.cancel();
    }
}
}

第九步App

public class App extends Application {
private static App        instance;
@Override
public void onCreate() {
    super.onCreate();
    T.init(this);
    instance = this;
}

public static App getInstance() {
    return instance;
}
}

第十步:配置文件:

activity_main.xml

toast_view_alertdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lLayout_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/toast_alert_bg"
android:orientation="vertical" >

<TextView
    android:id="@+id/txt_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="18sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/txt_msg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="16sp" />

<EditText
    android:id="@+id/edittxt_result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:gravity="center"
    android:hint="请输入信息"
    android:textColor="@color/black"
    android:textSize="16sp" />

<LinearLayout
    android:id="@+id/dialog_Group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
</LinearLayout>

<ImageView
    android:id="@+id/dialog_marBottom"
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:layout_marginTop="10dp"
    android:background="@color/alertdialog_line" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_neg"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_weight="1"
        android:background="@drawable/alertdialog_left_selector"
        android:gravity="center"
        android:textColor="@color/actionsheet_blue"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/img_line"
        android:layout_width="0.5dp"
        android:layout_height="43dp"
        android:background="@color/alertdialog_line" />

    <Button
        android:id="@+id/btn_pos"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_weight="1"
        android:background="@drawable/alertdialog_right_selector"
        android:gravity="center"
        android:textColor="@color/actionsheet_blue"
        android:textSize="16sp"
        android:textStyle="bold" />
</LinearLayout>

  </LinearLayout>

base_colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<color name="white">#FFFFFF</color>
<!-- 白色 -->
<color name="ivory">#FFFFF0</color>
<!-- 象牙色 -->
<color name="lightyellow">#FFFFE0</color>
<!-- 亮黄色 -->
<color name="yellow">#FFFF00</color>
<!-- 黄色 -->
<color name="snow">#FFFAFA</color>
<!-- 雪白色 -->
<color name="floralwhite">#FFFAF0</color>
<!-- 花白色 -->
<color name="lemonchiffon">#FFFACD</color>
<!-- 柠檬绸色 -->
<color name="cornsilk">#FFF8DC</color>
<!-- 米绸色 -->
<color name="seashell">#FFF5EE</color>
<!-- 海贝色 -->
<color name="lavenderblush">#FFF0F5</color>
<!-- 淡紫红 -->
<color name="papayawhip">#FFEFD5</color>
<!-- 番木色 -->
<color name="blanchedalmond">#FFEBCD</color>
<!-- 白杏色 -->
<color name="mistyrose">#FFE4E1</color>
<!-- 浅玫瑰色 -->
<color name="bisque">#FFE4C4</color>
<!-- 桔黄色 -->
<color name="moccasin">#FFE4B5</color>
<!-- 鹿皮色 -->
<color name="navajowhite">#FFDEAD</color>
<!-- 纳瓦白 -->
<color name="peachpuff">#FFDAB9</color>
<!-- 桃色 -->
<color name="gold">#FFD700</color>
<!-- 金色 -->
<color name="pink">#FFC0CB</color>
<!-- 粉红色 -->
<color name="lightpink">#FFB6C1</color>
<!-- 亮粉红色 -->
<color name="orange">#FFA500</color>
<!-- 橙色 -->
<color name="lightsalmon">#FFA07A</color>
<!-- 亮肉色 -->
<color name="darkorange">#FF8C00</color>
<!-- 暗桔黄色 -->
<color name="coral">#FF7F50</color>
<!-- 珊瑚色 -->
<color name="hotpink">#FF69B4</color>
<!-- 热粉红色 -->
<color name="tomato">#FF6347</color>
<!-- 西红柿色 -->
<color name="orangered">#FF4500</color>
<!-- 红橙色 -->
<color name="deeppink">#FF1493</color>
<!-- 深粉红色 -->
<color name="fuchsia">#FF00FF</color>
<!-- 紫红色 -->
<color name="magenta">#FF00FF</color>
<!-- 红紫色 -->
<color name="red">#FF0000</color>
<!-- 红色 -->
<color name="oldlace">#FDF5E6</color>
<!-- 老花色 -->
<color name="lightgoldenrodyellow">#FAFAD2</color>
<!-- 亮金黄色 -->
<color name="linen">#FAF0E6</color>
<!-- 亚麻色 -->
<color name="antiquewhite">#FAEBD7</color>
<!-- 古董白 -->
<color name="salmon">#FA8072</color>
<!-- 鲜肉色 -->
<color name="ghostwhite">#F8F8FF</color>
<!-- 幽灵白 -->
<color name="mintcream">#F5FFFA</color>
<!-- 薄荷色 -->
<color name="whitesmoke">#F5F5F5</color>
<!-- 烟白色 -->
<color name="beige">#F5F5DC</color>
<!-- 米色 -->
<color name="wheat">#F5DEB3</color>
<!-- 浅黄色 -->
<color name="sandybrown">#F4A460</color>
<!-- 沙褐色 -->
<color name="azure">#F0FFFF</color>
<!-- 天蓝色 -->
<color name="honeydew">#F0FFF0</color>
<!-- 蜜色 -->
<color name="aliceblue">#F0F8FF</color>
<!-- 艾利斯兰 -->
<color name="khaki">#F0E68C</color>
<!-- 黄褐色 -->
<color name="lightcoral">#F08080</color>
<!-- 亮珊瑚色 -->
<color name="palegoldenrod">#EEE8AA</color>
<!-- 苍麒麟色 -->
<color name="violet">#EE82EE</color>
<!-- 紫罗兰色 -->
<color name="darksalmon">#E9967A</color>
<!-- 暗肉色 -->
<color name="lavender">#E6E6FA</color>
<!-- 淡紫色 -->
<color name="lightcyan">#E0FFFF</color>
<!-- 亮青色 -->
<color name="burlywood">#DEB887</color>
<!-- 实木色 -->
<color name="plum">#DDA0DD</color>
<!-- 洋李色 -->
<color name="gainsboro">#DCDCDC</color>
<!-- 淡灰色 -->
<color name="crimson">#DC143C</color>
<!-- 暗深红色 -->
<color name="palevioletred">#DB7093</color>
<!-- 苍紫罗兰色 -->
<color name="goldenrod">#DAA520</color>
<!-- 金麒麟色 -->
<color name="orchid">#DA70D6</color>
<!-- 淡紫色 -->
<color name="thistle">#D8BFD8</color>
<!-- 蓟色 -->
<color name="lightgray">#D3D3D3</color>
<!-- 亮灰色 -->
<color name="lightgrey">#D3D3D3</color>
<!-- 亮灰色 -->
<color name="tan">#D2B48C</color>
<!-- 茶色 -->
<color name="chocolate">#D2691E</color>
<!-- 巧可力色 -->
<color name="peru">#CD853F</color>
<!-- 秘鲁色 -->
<color name="indianred">#CD5C5C</color>
<!-- 印第安红 -->
<color name="mediumvioletred">#C71585</color>
<!-- 中紫罗兰色 -->
<color name="silver">#C0C0C0</color>
<!-- 银色 -->
<color name="darkkhaki">#BDB76B</color>
<!-- 暗黄褐色 -->
<color name="rosybrown">#BC8F8F</color>
<!-- 褐玫瑰红 -->
<color name="mediumorchid">#BA55D3</color>
<!-- 中粉紫色 -->
<color name="darkgoldenrod">#B8860B</color>
<!-- 暗金黄色 -->
<color name="firebrick">#B22222</color>
<!-- 火砖色 -->
<color name="powderblue">#B0E0E6</color>
<!-- 粉蓝色 -->
<color name="lightsteelblue">#B0C4DE</color>
<!-- 亮钢兰色 -->
<color name="paleturquoise">#AFEEEE</color>
<!-- 苍宝石绿 -->
<color name="greenyellow">#ADFF2F</color>
<!-- 黄绿色 -->
<color name="lightblue">#ADD8E6</color>
<!-- 亮蓝色 -->
<color name="darkgray">#A9A9A9</color>
<!-- 暗灰色 -->
<color name="darkgrey">#A9A9A9</color>
<!-- 暗灰色 -->
<color name="brown">#A52A2A</color>
<!-- 褐色 -->
<color name="sienna">#A0522D</color>
<!-- 赭色 -->
<color name="darkorchid">#9932CC</color>
<!-- 暗紫色 -->
<color name="palegreen">#98FB98</color>
<!-- 苍绿色 -->
<color name="darkviolet">#9400D3</color>
<!-- 暗紫罗兰色 -->
<color name="mediumpurple">#9370DB</color>
<!-- 中紫色 -->
<color name="lightgreen">#90EE90</color>
<!-- 亮绿色 -->
<color name="darkseagreen">#8FBC8F</color>
<!-- 暗海兰色 -->
<color name="saddlebrown">#8B4513</color>
<!-- 重褐色 -->
<color name="darkmagenta">#8B008B</color>
<!-- 暗洋红 -->
<color name="darkred">#8B0000</color>
<!-- 暗红色 -->
<color name="blueviolet">#8A2BE2</color>
<!-- 紫罗兰蓝色 -->
<color name="lightskyblue">#87CEFA</color>
<!-- 亮天蓝色 -->
<color name="skyblue">#87CEEB</color>
<!-- 天蓝色 -->
<color name="gray">#808080</color>
<!-- 灰色 -->
<color name="grey">#808080</color>
<!-- 灰色 -->
<color name="olive">#808000</color>
<!-- 橄榄色 -->
<color name="purple">#800080</color>
<!-- 紫色 -->
<color name="maroon">#800000</color>
<!-- 粟色 -->
<color name="aquamarine">#7FFFD4</color>
<!-- 碧绿色 -->
<color name="chartreuse">#7FFF00</color>
<!-- 黄绿色 -->
<color name="lawngreen">#7CFC00</color>
<!-- 草绿色 -->
<color name="mediumslateblue">#7B68EE</color>
<!-- 中暗蓝色 -->
<color name="lightslategray">#778899</color>
<!-- 亮蓝灰 -->
<color name="lightslategrey">#778899</color>
<!-- 亮蓝灰 -->
<color name="slategray">#708090</color>
<!-- 灰石色 -->
<color name="slategrey">#708090</color>
<!-- 灰石色 -->
<color name="olivedrab">#6B8E23</color>
<!-- 深绿褐色 -->
<color name="slateblue">#6A5ACD</color>
<!-- 石蓝色 -->
<color name="dimgray">#696969</color>
<!-- 暗灰色 -->
<color name="dimgrey">#696969</color>
<!-- 暗灰色 -->
<color name="mediumaquamarine">#66CDAA</color>
<!-- 中绿色 -->
<color name="cornflowerblue">#6495ED</color>
<!-- 菊兰色 -->
<color name="cadetblue">#5F9EA0</color>
<!-- 军兰色 -->
<color name="darkolivegreen">#556B2F</color>
<!-- 暗橄榄绿 -->
<color name="indigo">#4B0082</color>
<!-- 靛青色 -->
<color name="mediumturquoise">#48D1CC</color>
<!-- 中绿宝石 -->
<color name="darkslateblue">#483D8B</color>
<!-- 暗灰蓝色 -->
<color name="steelblue">#4682B4</color>
<!-- 钢兰色 -->
<color name="royalblue">#4169E1</color>
<!-- 皇家蓝 -->
<color name="turquoise">#40E0D0</color>
<!-- 青绿色 -->
<color name="mediumseagreen">#3CB371</color>
<!-- 中海蓝 -->
<color name="limegreen">#32CD32</color>
<!-- 橙绿色 -->
<color name="darkslategray">#2F4F4F</color>
<!-- 暗瓦灰色 -->
<color name="darkslategrey">#2F4F4F</color>
<!-- 暗瓦灰色 -->
<color name="seagreen">#2E8B57</color>
<!-- 海绿色 -->
<color name="forestgreen">#228B22</color>
<!-- 森林绿 -->
<color name="lightseagreen">#20B2AA</color>
<!-- 亮海蓝色 -->
<color name="dodgerblue">#1E90FF</color>
<!-- 闪兰色 -->
<color name="midnightblue">#191970</color>
<!-- 中灰兰色 -->
<color name="aqua">#00FFFF</color>
<!-- 浅绿色 -->
<color name="cyan">#00FFFF</color>
<!-- 青色 -->
<color name="springgreen">#00FF7F</color>
<!-- 春绿色 -->
<color name="lime">#00FF00</color>
<!-- 酸橙色 -->
<color name="mediumspringgreen">#00FA9A</color>
<!-- 中春绿色 -->
<color name="darkturquoise">#00CED1</color>
<!-- 暗宝石绿 -->
<color name="deepskyblue">#00BFFF</color>
<!-- 深天蓝色 -->
<color name="darkcyan">#008B8B</color>
<!-- 暗青色 -->
<color name="teal">#008080</color>
<!-- 水鸭色 -->
<color name="green">#008000</color>
<!-- 绿色 -->
<color name="darkgreen">#006400</color>
<!-- 暗绿色 -->
<color name="blue">#0000FF</color>
<!-- 蓝色 -->
<color name="mediumblue">#0000CD</color>
<!-- 中兰色 -->
<color name="darkblue">#00008B</color>
<!-- 暗蓝色 -->
<color name="navy">#000080</color>
<!-- 海军色 -->
<color name="black">#000000</color>
<!-- 黑色 -->

</resources>

styles

  <resources>

<!-- Base application theme. -->
<!-- 去掉ActionBar,欲练此功,必先自宫 -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- 状态栏颜色 -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

<style name="AppTheme" parent="AppBaseTheme">

</style>


<!-- 自定义仿IOS的AlertDialog的样式 -->
<style name="AlertDialogStyle" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

  </resources>

color:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>

<!-- action背景 -->
<color name="action_color_blue">#048ad0</color>
<color name="action_color_alpha">#99ffffff</color>
<color name="action_color_blue_alpha">#9955a8fd</color>
<color name="btn_white">#FFFFFF</color>
<color name="btn_white_alpha">#66FFFFFF</color>
<color name="action_bg_gray">#f7f7f7</color>
<color name="action_bg_gray_alpha">#88f7f7f7</color>
<color name="gray_hint">#6655a8fd</color>
<color name="gray_interest">#B3B3B3</color>
<color name="gray_content">#999999</color>
<!-- 页面背景 -->
<color name="page_bg">#f2f2f2</color>
<!-- 页面背景 -->
<color name="gray_alp">#33000000</color>
<!-- 进度条绿色 -->
<color name="progress_green">#97d15c</color>
<color name="gray_alpha">#66000000</color>

<!-- 灰 -->
<color name="gray_btn">#e6e6e6</color>
<!-- 灰-线 -->
<color name="gray_line">#dddddd</color>
<!-- 灰-线 -->
<color name="gray_bg">#f0eff4</color>
<color name="red_txt">#ff6e6e</color>
<color name="line">#E5E5E5</color>
<color name="white1">#00ffffff</color>
<color name="white_found">#66ffffff</color>
<color name="green_radio_normal">#689869</color>

<color name="session_title">#222222</color>
<color name="session_time">#b8b8b8</color>
<color name="session_divider">#d9d9d9</color>
<color name="session_through_content">#888888</color>
<color name="session_draft">#ff6666</color>
<color name="chatting_item_divider_bg">#ebebeb</color>
<color name="text_color_gray">#ABABAB</color>
<color name="unread_tv_color">#007aff</color>
<color name="chat_link_color">#2fa9ff</color>
<color name="chat_msg_title">#545759</color>
<color name="chat_text_color">#222222</color>
<color name="login_divider_pwd">#bacfd9</color>
<color name="btn_normal">#0066cc</color>
<color name="btn_disable">#99c2eb</color>
<color name="choose_btn_disable">#b6d9f1</color>
<color name="btn_pressed">#1a5992</color>
<color name="hint_text_color_login_pwd">#a7b4b8</color>
<color name="login_divider">#a9a9a9</color>
<color name="text_color_light_gray">#777777</color>
<color name="text_color_dark">#222222</color>
<color name="border">#d9d9d9</color>
<color name="tips_bg">#2aa3e9</color>
<color name="session_content">#545759</color>
<color name="chat_input_editTextHintColor">#ebebeb</color>
<color name="chat_send_button_disabled">#949495</color>
<color name="chat_send_button_enabled">#2fa9ff</color>
<color name="text_color_tab">#03A9f4</color>
<!--popup_menu color-->
<color name="transparent_background_button">#11000000</color>
<color name="transparent_background">#ffffff</color>
<!--tab layout color-->
<color name="tab_un_selected">#818181</color>
<color name="refuse_btn">#f37700</color>

<!--listview 删除按钮背景色-->
<color name="sign_red">#f93c37</color>
<color name="sign_cancel">#d9d9d9</color>
<!--dialog 取消颜色-->
<color name="dialog_cancel">#7e7e7e</color>

<color name="backgroundCard">#FFF5F5F5</color>
<color name="textColorBg">#eee</color>
<color name="textColor">#757575</color>
<color name="image_bg">#49A8FC</color>
<color name="action_text_color">#00ff00</color>

<color name="alertdialog_line">#c6c6c6</color>
<color name="actionsheet_blue">#037BFF</color>
<color name="actionsheet_red">#FD4A2E</color>
<color name="actionsheet_gray">#8F8F8F</color>
</resources>

drawable资源我会放发githup 上

总结

好的,这就关于 实时监听网络状态内容,希望可以对你有帮助,喜欢的话请不吝点赞,感谢
githup地址:https://github.com/xuezhihuixzh/networkmonitordemo.git

上一篇下一篇

猜你喜欢

热点阅读