我对Android比较陌生,
我已阅读有关检测网络连接更改的相关文章并实现了此BroadcastReceiver
子类,对 AndroidManifest.xml 进行了必要的添加,并且按预期收到了必要的状态更改广播:
public class NetworkStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
问题是:如何在我的Activity
子类中接收或转发这些通知?NetworkStateReceiver
显然,在我的子类中创建一个实例Activity
并覆盖onReceive
那里并不能解决问题。
在此先感谢您的任何指点...
编辑:
我最终像这样Intent
从onReceive
上面广播了一个:
Intent target = new Intent(CONNECTIVITY_EVENT);
target.putExtra(CONNECTIVITY_STATE, networkInfo.isConnected());
context.sendBroadcast(target);
并以我的方式接收它Activity
:
@Override
protected String[] notifyStrings() {
return ArrayUtils.addAll(super.notifyStrings(), new String[] {NetworkStateReceiver.CONNECTIVITY_EVENT});
}
@Override
protected void notifyEvent(Intent intent, String action) {
super.notifyEvent(intent, action);
if (action != null) {
if (action.equalsIgnoreCase(NetworkStateReceiver.CONNECTIVITY_EVENT)) {
boolean isConnected = intent.getBooleanExtra(NetworkStateReceiver.CONNECTIVITY_STATE, true);
// Do something...
}
}
}