如何访问清单中声明的广播接收器并更改一些 属性
How to access broadcast receiver declared in manifest and change some property
我在AndroidManifest.xml中声明了一个接收器:
<receiver android:name=".receivers.ConnectionUpdateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
public class ConnectionUpdateReceiver extends BroadcastReceiver {
private Listener listener;
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo != null && networkInfo.isConnected()) {
if (listener != null) {
listener.onWifiConnected(networkInfo);
}
}
}
public interface Listener {
void onWifiConnected(NetworkInfo networkInfo);
}
}
我必须为此注册一个监听器 class。但是我不知道如何在我的 Activity class 中访问它来设置监听器 属性。
我尝试在运行时以通常的方式在我的 activity 的 onResume 中注册此接收器:
registerReceiver(connectionUpdateReceiver, new IntentFilter(...));
但我不知道将什么传递给 IntenetFilter 构造函数,因为
android.net.conn.CONNECTIVITY_CHANGE
那里无法访问。
要侦听网络更改事件,您的 activity 必须注册侦听 CONNECTIVITY_CHANGE 的广播接收器:
例如:
public void onCreate(Bundle savedInstanceState) {
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Hey, net?");
}}, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
}
希望对您的问题有所支持。
I have to register a listener to this class
这是不可能的,因为在 Android 发送并处理匹配的广播之前,没有清单注册 BroadcastReceiver
的实例。
I tried to register this receiver at runtime in the usually way in onResume of my activity:
只有去掉清单条目才有意义。
But I don't know what to pass to IntenetFilter constructor, since android.net.conn.CONNECTIVITY_CHANGE
is not accessible there.
欢迎您将"android.net.conn.CONNECTIVITY_CHANGE"
传递给IntentFilter
构造函数。它也可以作为 ConnectivityManager.CONNECTIVITY_ACTION
.
我在AndroidManifest.xml中声明了一个接收器:
<receiver android:name=".receivers.ConnectionUpdateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
public class ConnectionUpdateReceiver extends BroadcastReceiver {
private Listener listener;
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo != null && networkInfo.isConnected()) {
if (listener != null) {
listener.onWifiConnected(networkInfo);
}
}
}
public interface Listener {
void onWifiConnected(NetworkInfo networkInfo);
}
}
我必须为此注册一个监听器 class。但是我不知道如何在我的 Activity class 中访问它来设置监听器 属性。
我尝试在运行时以通常的方式在我的 activity 的 onResume 中注册此接收器:
registerReceiver(connectionUpdateReceiver, new IntentFilter(...));
但我不知道将什么传递给 IntenetFilter 构造函数,因为
android.net.conn.CONNECTIVITY_CHANGE
那里无法访问。
要侦听网络更改事件,您的 activity 必须注册侦听 CONNECTIVITY_CHANGE 的广播接收器:
例如:
public void onCreate(Bundle savedInstanceState) {
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Hey, net?");
}}, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
}
希望对您的问题有所支持。
I have to register a listener to this class
这是不可能的,因为在 Android 发送并处理匹配的广播之前,没有清单注册 BroadcastReceiver
的实例。
I tried to register this receiver at runtime in the usually way in onResume of my activity:
只有去掉清单条目才有意义。
But I don't know what to pass to IntenetFilter constructor, since
android.net.conn.CONNECTIVITY_CHANGE
is not accessible there.
欢迎您将"android.net.conn.CONNECTIVITY_CHANGE"
传递给IntentFilter
构造函数。它也可以作为 ConnectivityManager.CONNECTIVITY_ACTION
.