广播接收器在某些设备上不在后台工作
Broadcast receiver not working background on some devices
我正在尝试使用广播接收器获取 WiFi 连接更改事件 class。我已经在 Samsung S7 edge 和 HTC 626Q 上测试过它,它运行良好(即使应用程序关闭),但如果应用程序关闭,这不适用于 Huawei GR5 2017。仅当应用程序在屏幕上显示 运行 时才有效。 S7 和 GR5 设备都有 Android 7 (Nougat) OS.
广播接收器
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,intent.getAction(),Toast.LENGTH_SHORT).show();
}
}
Android清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="chanakamadurangakarunarathne.testbroadcastevents">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".NetworkChangeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.nsd.STATE_CHANGED" />
<action android:name="android.net.wifi.NETWORK_IDS_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.p2p.CONNECTION_STATE_CHANGE" />
<action android:name="android.net.wifi.p2p.DISCOVERY_STATE_CHANGE" />
<action android:name="android.net.wifi.p2p.PEERS_CHANGED" />
<action android:name="android.net.wifi.p2p.STATE_CHANGED" />
<action android:name="android.net.wifi.p2p.THIS_DEVICE_CHANGED" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.supplicant.STATE_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
我知道我可以使用服务,但如果我可以为所有设备使用广播接收器就好了。
我发现了很多关于 "Broadcast Receiver not working background" 的问题,但找不到我的问题的答案。
为什么会发生这种情况,有没有办法克服这个问题(仅使用广播接收器)?
您可能需要将您的应用程序添加到 "protected" 应用程序列表,或允许 运行 在后台运行的应用程序列表。在许多低端设备,以及来自小米、华为等的设备上 Android 将不会自动启动(或重启)应用程序,除非它们已明确添加到此列表中。查看电池、电源管理或安全下的设置。
有关详细信息,请参阅以下内容:
我正在尝试使用广播接收器获取 WiFi 连接更改事件 class。我已经在 Samsung S7 edge 和 HTC 626Q 上测试过它,它运行良好(即使应用程序关闭),但如果应用程序关闭,这不适用于 Huawei GR5 2017。仅当应用程序在屏幕上显示 运行 时才有效。 S7 和 GR5 设备都有 Android 7 (Nougat) OS.
广播接收器
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,intent.getAction(),Toast.LENGTH_SHORT).show();
}
}
Android清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="chanakamadurangakarunarathne.testbroadcastevents">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".NetworkChangeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.nsd.STATE_CHANGED" />
<action android:name="android.net.wifi.NETWORK_IDS_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.p2p.CONNECTION_STATE_CHANGE" />
<action android:name="android.net.wifi.p2p.DISCOVERY_STATE_CHANGE" />
<action android:name="android.net.wifi.p2p.PEERS_CHANGED" />
<action android:name="android.net.wifi.p2p.STATE_CHANGED" />
<action android:name="android.net.wifi.p2p.THIS_DEVICE_CHANGED" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.supplicant.STATE_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
我知道我可以使用服务,但如果我可以为所有设备使用广播接收器就好了。 我发现了很多关于 "Broadcast Receiver not working background" 的问题,但找不到我的问题的答案。 为什么会发生这种情况,有没有办法克服这个问题(仅使用广播接收器)?
您可能需要将您的应用程序添加到 "protected" 应用程序列表,或允许 运行 在后台运行的应用程序列表。在许多低端设备,以及来自小米、华为等的设备上 Android 将不会自动启动(或重启)应用程序,除非它们已明确添加到此列表中。查看电池、电源管理或安全下的设置。
有关详细信息,请参阅以下内容: