欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android仿QQ微信實(shí)時(shí)監(jiān)測(cè)網(wǎng)絡(luò)狀態(tài)

 更新時(shí)間:2018年05月29日 08:55:03   作者:zpf_  
這篇文章主要為大家詳細(xì)介紹了Android仿QQ微信實(shí)時(shí)監(jiān)測(cè)網(wǎng)絡(luò)狀態(tài),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

先簡(jiǎn)單說(shuō)一下思路:網(wǎng)絡(luò)變化時(shí)系統(tǒng)會(huì)發(fā)出廣播。所以我們監(jiān)聽(tīng)這個(gè)廣播,利用接口回調(diào)通知activity做相應(yīng)的操作就好了。

思路

  • 判斷網(wǎng)絡(luò)狀態(tài)(寫(xiě)個(gè)工具類NetUtil)
  • 寫(xiě)個(gè)類繼承BroadcastReceiver(不要忘記在清單文件中注冊(cè))
  • 需要在清單文件中添加權(quán)限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
  • 回調(diào)接口(NetEvevt)
  • BaseActivity實(shí)現(xiàn)這個(gè)接口

直接上代碼

NetUtil

import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
 
/** 
 * Author: lenovo 
 * Date: 2018/2/26 
 * email: 1271396448@qq.com 
 */ 
 
public class NetUtil { 
 
 
 /** 
  * 沒(méi)有連接網(wǎng)絡(luò) 
  */ 
 public static final int NETWORK_NONE = -1; 
 /** 
  * 移動(dòng)網(wǎng)絡(luò) 
  */ 
 public static final int NETWORK_MOBILE = 0; 
 /** 
  * 無(wú)線網(wǎng)絡(luò) 
  */ 
 public static final int NETWORK_WIFI = 1; 
 
 public static int getNetWorkState(Context context) { 
 
  // 得到連接管理器對(duì)象 
  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; 
   } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) { 
    return NETWORK_MOBILE; 
   } 
  } else { 
   return NETWORK_NONE; 
  } 
  return NETWORK_NONE; 
 } 
 
} 

NetBroadReceiver

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
 
/** 
 * Author: lenovo 
 * Date: 2018/2/26 
 * email: 1271396448@qq.com 
 */ 
 
public class NetBroadcastReceiver extends BroadcastReceiver { 
 public NetEvevt evevt = BaseActivity.evevt; 
 
 @Override 
 public void onReceive(Context context, Intent intent) { 
  // TODO Auto-generated method stub 
  // 如果相等的話就說(shuō)明網(wǎng)絡(luò)狀態(tài)發(fā)生了變化 
  if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { 
   int netWorkState = NetUtil.getNetWorkState(context); 
   // 接口回調(diào)傳過(guò)去狀態(tài)的類型 
   evevt.onNetChange(netWorkState); 
  } 
 } 
 
 // 自定義接口 
 public interface NetEvevt { 
  public void onNetChange(int netMobile); 
 } 
} 

BaseActivity

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
 
/** 
 * Author: lenovo 
 * Date: 2018/2/26 
 * email: 1271396448@qq.com 
 */ 
 
abstract public class BaseActivity extends FragmentActivity implements NetBroadcastReceiver.NetEvevt { 
 
 public static NetBroadcastReceiver.NetEvevt evevt; 
 /** 
  * 網(wǎng)絡(luò)類型 
  */ 
 private int netMobile; 
 
 @Override 
 protected void onCreate(Bundle arg0) { 
  // TODO Auto-generated method stub 
  super.onCreate(arg0); 
  evevt = this; 
  inspectNet(); 
 } 
 
 
 /** 
  * 初始化時(shí)判斷有沒(méi)有網(wǎng)絡(luò) 
  */ 
 
 public boolean inspectNet() { 
  this.netMobile = NetUtil.getNetWorkState(BaseActivity.this); 
  return isNetConnect(); 
 
  // if (netMobile == 1) { 
  // System.out.println("inspectNet:連接wifi"); 
  // } else if (netMobile == 0) { 
  // System.out.println("inspectNet:連接移動(dòng)數(shù)據(jù)"); 
  // } else if (netMobile == -1) { 
  // System.out.println("inspectNet:當(dāng)前沒(méi)有網(wǎng)絡(luò)"); 
  // 
  // } 
 } 
 
 /** 
  * 網(wǎng)絡(luò)變化之后的類型 
  */ 
 @Override 
 public void onNetChange(int netMobile) { 
  // TODO Auto-generated method stub 
  this.netMobile = netMobile; 
  isNetConnect(); 
 
 } 
 
 /** 
  * 判斷有無(wú)網(wǎng)絡(luò) 。 
  * 
  * @return true 有網(wǎng), false 沒(méi)有網(wǎng)絡(luò). 
  */ 
 public boolean isNetConnect() { 
  if (netMobile == 1) { 
   return true; 
  } else if (netMobile == 0) { 
   return true; 
  } else if (netMobile == -1) { 
   return false; 
 
  } 
  return false; 
 } 
 
} 

MainActivity

import android.os.Bundle; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.TextView; 
 
public class MainActivity extends BaseActivity { 
 
 private TextView textView,txtView; 
 ListView listView; //聲明一個(gè)ListView對(duì)象 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  textView= (TextView) findViewById(R.id.textView); 
  txtView=(TextView)this.findViewById(R.id.txtView); 
 
 
  //啟動(dòng)時(shí)判斷網(wǎng)絡(luò)狀態(tài) 
  boolean netConnect = this.isNetConnect(); 
  if (netConnect){ 
   textView.setVisibility(View.GONE); 
   txtView.setText("網(wǎng)絡(luò)連接正常"); 
  }else { 
   textView.setVisibility(View.VISIBLE); 
   txtView.setText("網(wǎng)絡(luò)連接異常"); 
  } 
 } 
 
 @Override 
 public void onNetChange(int netMobile) { 
  super.onNetChange(netMobile); 
  //網(wǎng)絡(luò)狀態(tài)變化時(shí)的操作 
  if (netMobile==NetUtil.NETWORK_NONE){ 
   textView.setVisibility(View.VISIBLE); 
   txtView.setText("網(wǎng)絡(luò)連接異常"); 
  }else { 
   textView.setVisibility(View.GONE); 
   txtView.setText("網(wǎng)絡(luò)連接正常"); 
  } 
 } 
 
} 

記得在清單文件中注冊(cè)

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論