Android判斷網路連接狀態

來源:互聯網
上載者:User

標籤:

  • 需要相關許可權

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

 

  • 在介面中,存在一個按鈕,單擊按鈕的情況下,判斷當前的網路連接狀態,分別在串連網路狀態和未串連網路狀態下,提示相關資訊。代碼如下:

public class MainActivity extends Activity {

    private Button button;//對應的按鈕
    private ConnectivityManager connetivityManager;//
    private NotificationManager notificationManager;// 通知使用者網路狀況
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context contex, Intent intent) {
            // TODO Auto-generated method stub
            // 網路狀況
            NetworkInfo mobileInfo = connetivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiInfo = connetivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            Notification.Builder builder = new Notification.Builder(
                    MainActivity.this);
            builder.setContentTitle("提示資訊");
            builder.setSmallIcon(R.drawable.ic_launcher);
            if (!mobileInfo.isConnected() && !wifiInfo.isConnected()) {
                builder.setContentText("網路狀態有異常");
            } else {
                builder.setContentText("網路正常");
            }
            notificationManager.notify(1001, builder.build());
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
                registerReceiver(broadcastReceiver, intentFilter);
                connetivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            }
        });
    }

    // 卸載廣播
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver);
        }
    }
}

Android判斷網路連接狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.