android 通過廣播監聽網路連接狀況

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   java   

首先添加以下許可權

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

new一個廣播NetStateChangeBroadcast

package aviationboss.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.net.ConnectivityManager;import android.net.NetworkInfo.State;import android.util.Log;import android.widget.Toast;import aviationboss.data.SystemData;public class NetStateChangeBroadcast extends BroadcastReceiver {    private ConnectivityManager connectivity;    private boolean isConnect = false;    @Override    public void onReceive(Context arg0, Intent arg1) {        // TODO Auto-generated method stub        connectivity = (ConnectivityManager) arg0                .getSystemService(arg0.CONNECTIVITY_SERVICE);        State wifiState = connectivity.getNetworkInfo(                ConnectivityManager.TYPE_WIFI).getState();        if (State.CONNECTED == wifiState) {            isConnect = true;        }                if(connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)!=null){            State mobileState = connectivity.getNetworkInfo(                    ConnectivityManager.TYPE_MOBILE).getState();            if (State.CONNECTED == mobileState) {                isConnect = true;            }        }                Intent intent = new Intent();        intent.setAction("netConnect");        if (isConnect) {            if (!SystemData.NetState) {                Toast.makeText(arg0, "網路已串連", Toast.LENGTH_SHORT).show();                SystemData.NetState = true;                intent.putExtra("netActions", "網路已串連");                arg0.sendBroadcast(intent);            }        } else {            if (SystemData.NetState) {                Toast.makeText(arg0, "網路已斷開", Toast.LENGTH_SHORT).show();                SystemData.NetState = false;                intent.putExtra("netActions", "網路已斷開");                arg0.sendBroadcast(intent);            }        }    }}

判斷wifi的串連狀態

connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()

判斷移動網的串連狀態

connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()

 

在設定檔中添加該廣播,並設定監聽的動作

<receiver android:name="aviationboss.broadcast.NetStateChangeBroadcast" >    <intent-filter>        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />    </intent-filter></receiver>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.