广播接收器在某些设备上无法正常工作 [英] Broadcast receiver not working background on some devices

查看:78
本文介绍了广播接收器在某些设备上无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Broadcast Receiver类获取WiFi连接更改事件.我已经在Samsung S7 edge和HTC 626Q上对其进行了测试,并且效果很好(即使关闭了应用程序也是如此),但是如果关闭了应用程序,这在Huawei GR5 2017上将无法正常工作.仅当应用程序在屏幕上运行时,它才有效. S7和GR5设备都具有Android 7(牛轧糖)操作系统.

I am trying to get WiFi connection changing event using Broadcast Receiver class. I have tested this on Samsung S7 edge and HTC 626Q and it works well (even when the app is closed), but this is not working on Huawei GR5 2017 if the app is closed. It works only when the app is running on screen. Both S7 and GR5 devices have 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();
    }
}

AndroidManifest文件

<?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>

我知道我可以使用服务,但是如果我可以在所有设备上使用广播接收器,那就太好了. 我发现了很多有关广播接收器无法正常工作的背景"的问题,但是找不到我的问题的答案. 为什么会发生这种情况,并且有解决方法(仅使用广播接收器)?

I know I can use Services, but It would be nice if I can use Broadcast Receiver for all devices. I found a lot of questions about "Broadcast Receiver not working background" but I couldn't find an answer for my question. Why this happens and is there a way to overcome this (Using Broadcast Receiver only)?

推荐答案

您可能需要将应用程序添加到受保护"应用程序列表中,或者将其添加到允许在后台运行的应用程序列表中.在许多低端设备以及小米,华为等设备上.除非将应用程序明确添加到此列表中,否则它们不会自动启动(或重新启动)应用程序.在电池",电源管理"或安全性"下查看设置".

You probably need to add your app to the list of "protected" apps, or the list of apps that are allowed to run in the background. On many low-end devices, and devices from Xiaomi, Huawei, etc. Android will not start (or restart) apps automatically unless they have been explicitly added to this list. Look in Settings under Battery, Power Management or Security.

有关更多信息,请参见以下内容:

See the following for more info:

  • BroadcastReceiver works for a while, then stops
  • "Protected Apps" setting on Huawei phones, and how to handle it

这篇关于广播接收器在某些设备上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆