我有一个库,其中包括一个名为BaseActivity
的活动和一个名为BaseRegister
的接收器。
BaseRegister
扩展了BroadcastReceiver
,其操作是android.net.conn.CONNECTIVITY_CHANGE
和android.net.wifi.WIFI_STATE_CHANGED
,它看起来像这样:
public class BaseRegister extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if {
Log.d("onReceive", "Got it"); // Works
context.sendBroadcast(new Intent("some"));
else {
Log.d("onReceive", "Nope"); // Works
context.sendBroadcast(new Intent("stuff"));
}
}
}
AndroidManifest
也可以,根据 Log
的说法。下面是 BaseActivity
的样子:
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(applicationControl, new IntentFilter("some"));
registerReceiver(applicationControl, new IntentFilter("stuff"));
}
private BroadcastReceiver applicationControl = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.equals(new Intent("some"))) {
some();
} else if (intent.equals(new Intent("stuff"))) {
stuff();
}
}
};
public void some() { /** logging etc **/ }
public void stuff() { /** logging etc **/ }
}
在项目中,我已经将此项目添加为库,没有问题。并创建了一个扩展
BaseActivity
的活动。当CONNECTIVITY_CHANGE
作为操作时,Android触发BaseRegister
,但活动没有调用。以下是我的项目外观:public class AnyActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void some() {
super.some();
Log.d("abc", "abc"); // not working
}
@Override
public void stuff() {
super.stuff();
Log.d("abc", "abc"); //not working
}
}
有什么问题吗?我的方法不当还是存在错误?任何帮助都将不胜感激。
AnyActivity
在接收到广播时是处于活动状态吗? - Gopal Gopi