标签:instance ring layout etc lte filter gis smo show
版权声明:本文为博主原创文章,未经博主允许不得转载。
用来监控网络变化时的处理,一般用来toast提示或者扩展为其他作用。
NetStateReceiver:网络改变时的广播器
NetUtils:获取网络类型
注意事项:
1、导入类文件后需要change包名以及重新import R文件路径
2、Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.why.project.netstatereceiverdemo"> <!-- ======================NetStateReceiver相关========================== --> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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> <!-- ===========NetStateReceiver相关=====也可以使用动态注册在MyApplication,不过需要注意注销的触发时机=============== --> <receiver android:name=".utils.netstatus.NetStateReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> <action android:name="os.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver> </application> </manifest>
package com.why.project.netstatereceiverdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.why.project.netstatereceiverdemo.utils.ToastUtil; import com.why.project.netstatereceiverdemo.utils.netstatus.NetChangeObserver; import com.why.project.netstatereceiverdemo.utils.netstatus.NetStateReceiver; import com.why.project.netstatereceiverdemo.utils.netstatus.NetUtils; /** * Created by HaiyuKing * Used Activity基类 */ public abstract class BaseActivity extends AppCompatActivity { private static final String TAG = "BaseActivity"; /*=================网络变化的广播器=====================*/ protected NetChangeObserver mNetChangeObserver; @Override protected void onCreate(Bundle savedInstanceState) { /*=================网络变化的广播器=====================*/ registerNetStateChange();//注册网络变化的广播器 super.onCreate(savedInstanceState); } protected void onDestroy() { super.onDestroy(); /*=================网络变化的广播器=====================*/ NetStateReceiver.removeRegisterObserver(this.mNetChangeObserver); } /*==========网络变化的广播器=============*/ private void registerNetStateChange() { if (this.mNetChangeObserver == null) { this.mNetChangeObserver = new NetChangeObserver() { public void onNetConnected(NetUtils.NetType netType) { super.onNetConnected(netType); onNetworkConnected(netType); if (NetUtils.isMobileConnected(getApplicationContext())){ ToastUtil.showShortToast("您当前正在使用运营商网络"); } if(NetUtils.isWifiConnected(getApplicationContext())){ ToastUtil.showShortToast("您当前正在使用无线网络"); } } public void onNetDisConnect() { super.onNetDisConnect(); onNetworkDisConnected(); ToastUtil.showShortToast("网络无法连接,请检查网络设置"); } }; NetStateReceiver.registerObserver(this.mNetChangeObserver);// 声明mNetChangeObserver注册到网络状态广播接收器中 } } protected abstract void onNetworkConnected(NetUtils.NetType netType); protected abstract void onNetworkDisConnected(); }
本Demo搭配《ToastUtil【简单的Toast封装类】【未自定义Toast的显示风格】》使用
package com.why.project.netstatereceiverdemo; import android.os.Bundle; import com.why.project.netstatereceiverdemo.utils.netstatus.NetUtils; public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onNetworkConnected(NetUtils.NetType netType) { } @Override protected void onNetworkDisConnected() { } }
无
http://www.cnblogs.com/renzimu/p/4537954.html
http://www.cnblogs.com/renzimu/p/4537936.html
https://github.com/haiyuKing/NetStateReceiverDemo
标签:instance ring layout etc lte filter gis smo show
原文地址:http://www.cnblogs.com/whycxb/p/7635299.html