私は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...
}
}
}