接每日一更之第一天(判断网络状态)
模仿扣扣的页面,后台判断网络状态 当网络状态改变的时候,能通知前台页面做出相应的操作。(页面很丑)
广播代码
public class NetWorkBroadcast extends BroadcastReceiver {
public static NetWorkListener netWorkListener;
@Override
public void onReceive(Context context, Intent intent) {
//如果无网络连接activeInfo为null
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo activeInfo = manager.getActiveNetworkInfo();
if(activeInfo == null||!activeInfo.isAvailable()){
netWorkListener.OnReceived("无网");
}else{
if(wifiInfo == null||!wifiInfo.isAvailable()){
netWorkListener.OnReceived("GPRS");
}else{
netWorkListener.OnReceived("wifi");
}
}
}
public interface NetWorkListener{
public void OnReceived(String net);
}
public void setOnReceivedMessageListener(NetWorkListener netWorkListener) {
this.netWorkListener=netWorkListener;
}
MainActivity 代码
public class NetworkActivity extends Activity {
Button btn;
TextView tv;
public static ConnectivityManager connectivityManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
tv = (TextView) findViewById(R.id.tv);
/**
* 测试ConnectivityManager
* ConnectivityManager主要管理和网络连接相关的操作
* 相关的TelephonyManager则管理和手机、运营商等的相关信息;WifiManager则管理和wifi相关的信息。
* 想访问网络状态,首先得添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
* NetworkInfo类包含了对wifi和mobile两种网络模式连接的详细描述,通过其getState()方法获取的State对象则代表着
* 连接成功与否等状态。
*
*/
connectivityManager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetWorkBroadcast netWorkBroadcast = new NetWorkBroadcast();
//广播监听
netWorkBroadcast.setOnReceivedMessageListener(new NetWorkBroadcast.NetWorkListener() {
@Override
public void OnReceived(String net) {
if("无网".equals(net)){
btn.setVisibility(View.VISIBLE);
}else{
btn.setVisibility(View.INVISIBLE);
isNetworkAvailable();
}
}
});
if(checkNetworkState()){
btn.setVisibility(View.VISIBLE);
}else{
btn.setVisibility(View.INVISIBLE);
isNetworkAvailable();
}
//点击进入设置网络
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
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_WIFI_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);
}
});
}
//检查是否有网
private static boolean checkNetworkState() {
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo == null||!activeNetInfo.isAvailable()) {
return true;
}
return false;
}
/**
* 网络已经连接,然后去判断是wifi连接还是GPRS连接 设置一些自己的逻辑调用
*/
private void isNetworkAvailable() {
NetworkInfo.State gprs = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
NetworkInfo.State wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (gprs == NetworkInfo.State.CONNECTED || gprs == NetworkInfo.State.CONNECTING) {
Toast.makeText(this, "wifi is open! gprs", Toast.LENGTH_SHORT)
.show();
}
// 判断为wifi状态下才加载广告,如果是GPRS手机网络则不加载!
if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
Toast.makeText(this, "wifi is open! wifi", Toast.LENGTH_SHORT)
.show();
}
}
//设置完成后返回到本页面要进行再次判断网络状态
@Override
protected void onRestart() {
super.onRestart();
checkNetworkState();
}
main.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="@null"
android:text="设置网络连接"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="网络连接状态:"/>
</LinearLayout>
这样做的好处可以使你自己的程序时刻监听手机的网络状态 使页面做出相应的判断
最后徐还要添加权限和广播注册(静态注册和动态注册都可以建议是动态注册这样每个页面都可以使用此广播)