注册广播有两种方法:动态注册(在代码中注册),静态注册(AndroiMainfest.xml)。
实现注册你需要指明:要监听什么广播,和监听之后要做什么动作(自定义广播重写onReceive)。
举一个监听网络变化的例子
要监听的广播是:"android.net.conn.CONNECTIVITY_CHANGE"
收到此广播之后我们要发一个通知(Toast)
1.动态注册:
在MainActivity的onCreate()方法里修改:
package com.example.broadcasttest; import android.app.Activity; 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.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends Activity { private IntentFilter intentFilter; private NetworkChangeReceiver networkChangeReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); //添加要收到的广播 networkChangeReceiver = new NetworkChangeReceiver(); //广播实例 registerReceiver(networkChangeReceiver, intentFilter); //注册 } @Override public void onDestroy(){ super.onDestroy(); unregisterReceiver(networkChangeReceiver); } class NetworkChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent){ ConnectivityManager connectionManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo(); 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(); } } } }2。静态注册:
新建一个NetworkChangeReceiver类:
public class NetworkChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent){ ConnectivityManager connectionManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo(); 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(); } } }然后在AndroidMainfest.xml注册这个广播:
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name=<span style="color:#33FF33;">".NetworkChangeReceiver"</span>> <intent-filter> <action android:name=<span style="color:#33CC00;">"android.net.conn.CONNECTIVITY_CHANGE"</span>/> </intent-filter> </receiver> <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>别忘了在AndroidMainfest.xml中加上查询网络状态的权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>