注册广播的两种方式:1.使用Context.registerReceiver() 2.使用<receiver>在AndroidManifest.xml(清单文件)中注册
注册广播的时候在Activity.onResume()中注册,在Activity.onPause()中卸载
下面我们来介绍一下它的使用方法。
整体思路:在activity中的onCreate方法中声明ConnectivityManager对象,用于判断网络的状况,声明NotificationManager对象,用于发送状态栏通知,实例化一个BroadcastReceiver对象,在这个对象的onReceive方法中判断网络链接情况,如果数据网络和无线网络都没有连接,则弹出网络异常的状态栏通知。在onCreate方法中实例化ConnectivityManager对象和NotificationManager对象,在onResume方法中注册广播,在onPause方法中卸载广播。注意在清单文件AndroidManifest.xml中添加网络授权。
MainActivity.java文件:
package com.example.android_broadcast_state;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Notification.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
public class MainActivity extends Activity {
private ConnectivityManager manager;//判断网络的状况
private NotificationManager notificationManager;//需要通知去提醒用户
private BroadcastReceiver receiver=new BroadcastReceiver() {
@SuppressLint("NewApi") @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// 提醒用户网络状况有异常
// 分别获得2G和3G、wifi的网络信号
// 广播中提醒用户的两种方式:土司、通知
NetworkInfo mobileInfo=manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiInfo=manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(!mobileInfo.isConnected()||!wifiInfo.isConnected()){
Notification.Builder builder=new Notification.Builder(MainActivity.this);
builder.setContentText("提示信息");
builder.setContentText("网络状况有异常!");
builder.setSmallIcon(R.drawable.ic_launcher);
notificationManager.notify(1001,builder.build());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
// 注册广播
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter filter=new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(receiver, filter);
}
// 卸载广播
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(receiver!=null){
unregisterReceiver(receiver);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_broadcast_state"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- 添加网络授权 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android_broadcast_state.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>
</manifest>