我有一个广播接收器,它在注册时被触发(随后用 onPause / onResume 重新触发),这肯定是错误的行为吗?我在这里错过了什么吗?
class FooActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
broadcastReceiver = new FooBroadcastReceiver();
intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(connectivityReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(connectivityReceiver);
}
class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (connectivityAction(intent)) {
Log.d("footag", "onReceive");
}
}
private boolean connectivityAction(Intent intent) {
return ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction());
}
}