LocalBroadcastManager 能否检测到 WifiManager.NETWORK_STATE_CHANGED_ACTION 变化?
Can LocalBroadcastManager detect WifiManager.NETWORK_STATE_CHANGED_ACTION changes?
我想使用 BroadcastReceiver 通知我的 Activity 任何 Wifi 连接更改。由于此广播在应用程序内,我正在尝试使用更高效的 LocalBroadcastManager 对象。
但是无论我做什么,BroadcastReceiver.onReceive() 方法都不会触发。我可能没有正确连接它,或者我正在侦听的 WifiManager.NETWORK_STATE_CHANGED_ACTION 操作无法针对 LocalBroadcastManager 注册?任何帮助或澄清将不胜感激。
这是我的 Activity class 的示例,其中包含所有逻辑。
public class MyActivity extends ActionBarActivity {
private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
{
// Do something
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
IntentFilter wifiStatusIntentFilter = new IntentFilter();
wifiStatusIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
wifiStatusIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(wifiReceiver, wifiStatusIntentFilter);
}
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(wifiReceiver);
}
protected void onResume() {
super.onResume();
IntentFilter wifiStatusIntentFilter = new IntentFilter();
wifiStatusIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(wifiReceiver, wifiStatusIntentFilter);
}
}
当我打开和关闭手机上的 wifi,或者进入和离开 wifi 范围时,永远不会触发 onReceive() 方法。
Since this broadcast is within the application I'm trying to use the more efficient LocalBroadcastManager object.
这仅适用于您通过LocalBroadcastManager
发送的广播。它不适用于系统广播,尤其是其他进程发送的广播。
perhaps the WifiManager.NETWORK_STATE_CHANGED_ACTION action I'm listening for cannot be registered against a LocalBroadcastManager?
正确。
您无法使用 LocalBroadcastManager 接收 WifiManager.NETWORK_STATE_CHANGED_ACTION。 LocalBroadcastManager 仅在您的进程内工作。
Helper to register for and send broadcasts of Intents to local objects
within your process. This is has a number of advantages over sending
global broadcasts with sendBroadcast(Intent):
- You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.
- It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes
they can exploit.
- It is more efficient than sending a global broadcast through the system.
你应该使用 Context 的 registerReceiver
我想使用 BroadcastReceiver 通知我的 Activity 任何 Wifi 连接更改。由于此广播在应用程序内,我正在尝试使用更高效的 LocalBroadcastManager 对象。
但是无论我做什么,BroadcastReceiver.onReceive() 方法都不会触发。我可能没有正确连接它,或者我正在侦听的 WifiManager.NETWORK_STATE_CHANGED_ACTION 操作无法针对 LocalBroadcastManager 注册?任何帮助或澄清将不胜感激。
这是我的 Activity class 的示例,其中包含所有逻辑。
public class MyActivity extends ActionBarActivity {
private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
{
// Do something
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
IntentFilter wifiStatusIntentFilter = new IntentFilter();
wifiStatusIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
wifiStatusIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(wifiReceiver, wifiStatusIntentFilter);
}
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(wifiReceiver);
}
protected void onResume() {
super.onResume();
IntentFilter wifiStatusIntentFilter = new IntentFilter();
wifiStatusIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(wifiReceiver, wifiStatusIntentFilter);
}
}
当我打开和关闭手机上的 wifi,或者进入和离开 wifi 范围时,永远不会触发 onReceive() 方法。
Since this broadcast is within the application I'm trying to use the more efficient LocalBroadcastManager object.
这仅适用于您通过LocalBroadcastManager
发送的广播。它不适用于系统广播,尤其是其他进程发送的广播。
perhaps the WifiManager.NETWORK_STATE_CHANGED_ACTION action I'm listening for cannot be registered against a LocalBroadcastManager?
正确。
您无法使用 LocalBroadcastManager 接收 WifiManager.NETWORK_STATE_CHANGED_ACTION。 LocalBroadcastManager 仅在您的进程内工作。
Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent):
- You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.
- It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
- It is more efficient than sending a global broadcast through the system.
你应该使用 Context 的 registerReceiver