清单中声明的​​ BroadcastReceiver 未接收广播

发布于 2025-01-08 16:36:34 字数 1925 浏览 0 评论 0原文

我尝试注册一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

少钕鈤記 2025-01-15 16:36:35

您的清单中的接收器应如下所示

<receiver android:name=".WifiReceiver" >
    <intent-filter>
       <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

另外可能还需要以下权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Your receiver in manifest should looks like this

<receiver android:name=".WifiReceiver" >
    <intent-filter>
       <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

Also the following permission may be needed

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
清醇 2025-01-15 16:36:35

如果您的目标 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

再见回来 2025-01-15 16:36:35
<receiver android:name=".WifiReceiver" >
     <intent-filter android:priority="100" >
          <action
             android:name="android.net.wifi.WIFI_STATE_CHANGED"
             android:enabled="true" />
     </intent-filter>
</receiver>

输入高优先级并将启用标志设置为 true

<receiver android:name=".WifiReceiver" >
     <intent-filter android:priority="100" >
          <action
             android:name="android.net.wifi.WIFI_STATE_CHANGED"
             android:enabled="true" />
     </intent-filter>
</receiver>

Enter the high priority and also enabled flag as true

怪我入戏太深 2025-01-15 16:36:35

仅仅因为广播是用标志 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.

两相知 2025-01-15 16:36:35

我认为您已经像这样注册了接收者:

<receiver
    android:name=".WiFiReciever">
    <intent-filter>
        <action
            android:name="class name with package" />
        <data
            android:scheme="myscheme" />
    </intent-filter>
</receiver> 

I think you have registered the receiver like this way:

<receiver
    android:name=".WiFiReciever">
    <intent-filter>
        <action
            android:name="class name with package" />
        <data
            android:scheme="myscheme" />
    </intent-filter>
</receiver> 
风月客 2025-01-15 16:36:35

对我来说最好的:

清单:

<receiver android:name="com.AEDesign.communication.WifiReceiver">
    <intent-filter android:priority="100">
        <action android:name="android.net.wifi.STATE_CHANGE" />
    </intent-filter>
</receiver>

WifiReceiver 类:

public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if(info != null) {
            if(info.isConnected()) {
                //Do your work. 
                //To check the Network Name or other info:
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ssid = wifiInfo.getSSID();    
            }
        }
    }
}

权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

The best that worked for me:

Manifest:

<receiver android:name="com.AEDesign.communication.WifiReceiver">
    <intent-filter android:priority="100">
        <action android:name="android.net.wifi.STATE_CHANGE" />
    </intent-filter>
</receiver>

WifiReceiver Class:

public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if(info != null) {
            if(info.isConnected()) {
                //Do your work. 
                //To check the Network Name or other info:
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ssid = wifiInfo.getSSID();    
            }
        }
    }
}

Permissions:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
不及他 2025-01-15 16:36:35

这是与互联网连接相关的所有广播接收器

<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />                
<action android:name="android.net.wifi.STATE_CHANGE" />

This is all broadcast receiver related to the internet connection

<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />                
<action android:name="android.net.wifi.STATE_CHANGE" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文