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

Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)的改變

 更新時間:2018年05月29日 09:54:51   作者:huaxaY  
這篇文章主要為大家詳細(xì)介紹了Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)的改變,具有一定的參考價值,感興趣的小伙伴們可以參考一下

只需要實現(xiàn)下面2段代碼即可實現(xiàn)對網(wǎng)絡(luò)連接狀態(tài)的監(jiān)聽,千萬別忘了在Manifest.xml里面添加網(wǎng)絡(luò)訪問權(quán)限哦。

1、定義廣播接收器

NetState.java

package huaxa.it.map.net;

import huaxa.it.map.demo.MapDemo;

import com.baidu.mapapi.map.MapView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.util.Log;
import android.widget.Toast;

/**
 * @項目名: ZhiHUIGuangDong
 * @包名: huaxa.it.zhihuiguangdong
 * @類名: NetState
 * @創(chuàng)建者: 黃夏蓮
 * @創(chuàng)建時間: 2016年10月22日 ,上午1:10:16
 * 
 * @描述: TODO
 */
public class NetState extends BroadcastReceiver
{

 public int flag = 0;
 private int flag1=0;

 @Override
 public void onReceive(Context context, Intent arg1)
 {
  ConnectivityManager manager = (ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
  if (activeNetworkInfo == null)
  {
   Toast.makeText(context, "當(dāng)前無網(wǎng)絡(luò),請檢查移動設(shè)備的網(wǎng)絡(luò)連接", Toast.LENGTH_SHORT)
     .show();
   flag = 1;
   flag1 = 1;
   Log.i("TAG","網(wǎng)絡(luò)未連接+flag+"+flag);
  }
  // activeNetworkInfo.getTypeName(); 以何種方式連線
  // :cmwap/cmnet/wifi/uniwap/uninet
  // activeNetworkInfo.isAvailable(); 當(dāng)前網(wǎng)絡(luò)是否可用(true)
  // activeNetworkInfo.isFailover();網(wǎng)絡(luò)有問題
  else
  {
   if (!activeNetworkInfo.isAvailable()
     || activeNetworkInfo.isFailover())
   {
    Toast.makeText(context, "當(dāng)前網(wǎng)絡(luò)不可用", Toast.LENGTH_SHORT).show();
    flag = 1;
    flag1=2;
    Log.i("TAG","當(dāng)前網(wǎng)絡(luò)不可用flag+"+flag);
   }

   if (flag == 1)
   {
    if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
    {
     Toast.makeText(context, "已連接上移動數(shù)據(jù)", Toast.LENGTH_SHORT)
       .show();
    } else
    {
     Toast.makeText(context, "已連接上WIFI數(shù)據(jù)", Toast.LENGTH_SHORT)
       .show();
    }
    Log.i("TAG","網(wǎng)絡(luò)ok,flag+"+flag+"....."+flag1);
   }

  }
 }
}

2、注冊Broadcast Receiver

在Activity中加入:

NetState receiver = new NetState();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
this.registerReceiver(receiver, filter);//注冊
receiver.onReceive(this, null);//接收

Broadcast Receiver知識點

每收到一次消息時便調(diào)用onReceive(Context context,Intent intent)對其處理。

Intent可以傳遞數(shù)據(jù)實現(xiàn)自定義廣播事件,然后通過sendBroadcast(intent)發(fā)送出去。通過onReceive()方法處理接收的這個的數(shù)據(jù)。

String Action = "xxxxxx";//xxxxxx指的是自定義的Broadcast的action屬性,如com.annyAndroid.broadcastdemo.action.USER_BROADCAST,隨便定義,但是在廣播接收器的intentfilter中的action屬性必須和這個一致
Intent intent = new Intent();
intent.putExtra("user","huaxa");//key-Value
sendBroadcast(intent);

注意:如果在onReceive()方法中的代碼執(zhí)行時間超過5s的話,Android會ANR。

1、使用廣播接收器

public class AAAAA extends Broadcast Receiver{
 @Override
 onReceive(Context context,Intent intent){
 //處理廣播事件
 ......
 }
}

2、注冊Broadcast Receiver(2種方法)

1)、在Manifest.xml里面:

<receiver android:name=".MyReceiver"> 
   <intent-filter> 
    <action android:name="xxxxxxxx"/>
    <!--xxxxx廣播事件的Action屬性 -->
   </intent-filter> 
</receiver> 

2、在Activity中onCreate()用Java代碼實現(xiàn)

IntentFilter filter = new IntentFilter(xxxx);//添加事件的ACTION,如電池電量、網(wǎng)絡(luò)連接變化、有來電、短信息等
AAAAA aAAAA = new AAAAA();
registerReceiver(aAAAA,filter);//注冊

3)、在適當(dāng)時取消注冊Receiver,可以在程序中取消,在onDestroy()中調(diào)用如下函數(shù)即可:

if (connectionReceiver != null) {
 unregisterReceiver(connectionReceiver);
 }

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

相關(guān)文章

最新評論