本文實(shí)例為大家分享了Android學(xué)習(xí)之Broadcast的使用方法,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)專注于企業(yè)成都營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、海城網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為海城等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。實(shí)現(xiàn)開機(jī)啟動(dòng)提示網(wǎng)絡(luò)的廣播
package com.example.luobo.broadcasttest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private IntentFilter intentFilter; private NetworkChangeReceiver networkChangeReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intentFilter = new IntentFilter();//創(chuàng)建一個(gè)過濾器實(shí)例 intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");//添加接收CONNECTIVITY_CHANGE消息 networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver,intentFilter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(networkChangeReceiver); } class NetworkChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);//通過此方法獲取ConnectivityManager實(shí)例 NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();//調(diào)用實(shí)例connectivityManager的getActiveNetworkInfo()方法獲取NetworkInfo實(shí)例 if (networkInfo != null && networkInfo.isAvailable()){ Toast.makeText(context,"Network is available",Toast.LENGTH_SHORT).show(); }else { Toast.makeText(context,"Network is unavailable",Toast.LENGTH_SHORT).show(); } } } }
網(wǎng)頁(yè)標(biāo)題:Android學(xué)習(xí)之Broadcast的簡(jiǎn)單使用-創(chuàng)新互聯(lián)
標(biāo)題URL:http://aaarwkj.com/article20/ccdhco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)頁(yè)設(shè)計(jì)公司、用戶體驗(yàn)、網(wǎng)站收錄、Google、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容