清单中声明的 BroadcastReceiver 未接收广播
我尝试注册一个 Wifi BroadcastReceiver 以在 wifi 状态发生变化时获取它。但到目前为止我还没有收到广播。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".WifiReceiver" >
<intent-filter>
<action android:name="android.net.wifi.WifiManager.WIFI_STATE_CHANGED_ACTION" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
//activity declaration here...
</application>
你们知道如何在清单中注册 BraodcastReceiver 吗?
我不想在活动中注册它,因为我想在 wifi 状态发生变化时监视 wifi,无论我的应用程序是否正在运行。
这是我的 BroadcastReceiver 类:
public class WifiReceiver extends BroadcastReceiver {
private final String TAG = "WifiReceiver";
@Override
public void onReceive(Context context, Intent intent) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
String wifiStateText = "No State";
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLING:
wifiStateText = "WIFI_STATE_DISABLING";
break;
case WifiManager.WIFI_STATE_DISABLED:
wifiStateText = "WIFI_STATE_DISABLED";
break;
case WifiManager.WIFI_STATE_ENABLING:
wifiStateText = "WIFI_STATE_ENABLING";
break;
case WifiManager.WIFI_STATE_ENABLED:
wifiStateText = "WIFI_STATE_ENABLED";
break;
case WifiManager.WIFI_STATE_UNKNOWN:
wifiStateText = "WIFI_STATE_UNKNOWN";
break;
default:
break;
}
MyLog.d(TAG, "onReceive Broadcast > WiFiState: " + wifiStateText);
MyLog.d(TAG, "onReceive Broadcast > Time: " + new Date());
}
}
我真的希望得到一些帮助。提前致谢。
I've tried to register a Wifi BroadcastReceiver to get the wifi state when it changes. But so far I have no luck receiving the broadcast.
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".WifiReceiver" >
<intent-filter>
<action android:name="android.net.wifi.WifiManager.WIFI_STATE_CHANGED_ACTION" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
//activity declaration here...
</application>
Do you guys know how to register the BraodcastReceiver in manifest?
I don't want to register it in activities because I want to monitor the wifi when there is changes in the wifi state whether my application is running or not.
This is my BroadcastReceiver class:
public class WifiReceiver extends BroadcastReceiver {
private final String TAG = "WifiReceiver";
@Override
public void onReceive(Context context, Intent intent) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
String wifiStateText = "No State";
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLING:
wifiStateText = "WIFI_STATE_DISABLING";
break;
case WifiManager.WIFI_STATE_DISABLED:
wifiStateText = "WIFI_STATE_DISABLED";
break;
case WifiManager.WIFI_STATE_ENABLING:
wifiStateText = "WIFI_STATE_ENABLING";
break;
case WifiManager.WIFI_STATE_ENABLED:
wifiStateText = "WIFI_STATE_ENABLED";
break;
case WifiManager.WIFI_STATE_UNKNOWN:
wifiStateText = "WIFI_STATE_UNKNOWN";
break;
default:
break;
}
MyLog.d(TAG, "onReceive Broadcast > WiFiState: " + wifiStateText);
MyLog.d(TAG, "onReceive Broadcast > Time: " + new Date());
}
}
I really hope to get some help. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的清单中的接收器应如下所示
另外可能还需要以下权限
Your receiver in manifest should looks like this
Also the following permission may be needed
如果您的目标 android 版本高于 Android O。如果您在清单中声明接收器,它们将无法工作。所以你需要在你的活动中注册。
注意:如果您的应用程序以 API 级别 26 或更高级别为目标,则无法使用清单来声明隐式广播(不专门针对您的应用程序的广播)的接收器,但少数不受该限制的隐式广播除外。在大多数情况下,您可以使用计划作业。
资源:
https://developer.android.com/guide/components/broadcasts
If your Target android version is more than Android O. if you declare receivers in manifest they wont work. So you need to register inside your activity.
Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.
Resource:
https://developer.android.com/guide/components/broadcasts
输入高优先级并将启用标志设置为 true
Enter the high priority and also enabled flag as true
仅仅因为广播是用标志 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT 发送的,这意味着您的应用程序必须在 android 启动之前注册此接收器,这只能是系统服务。
Just because the broadcast was send with flag Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, it means, your app must register this receiver before android boot up, which can only be the system services.
我认为您已经像这样注册了接收者:
I think you have registered the receiver like this way:
对我来说最好的:
清单:
WifiReceiver 类:
权限:
The best that worked for me:
Manifest:
WifiReceiver Class:
Permissions:
这是与互联网连接相关的所有广播接收器
This is all broadcast receiver related to the internet connection