简体   繁体   English

onReceive()并不总是被调用

[英]onReceive() is not always called

For some reason, the onReceive() method of my BroadcastReceiver class is not always called. 由于某些原因,我的BroadcastReceiver类的onReceive()方法并不总是被调用。 It is usually called, but sometimes it is not. 它通常被称为,但有时不是。

More specifically, I am receiving android.intent.action.PHONE_STATE . 更具体地说,我正在接收android.intent.action.PHONE_STATE Most of the time, when a call comes in, the onRecieve() method is called as it should be for changes to RINGING, IDLE, and OFFHOOK. 在大多数情况下,当有电话打进来时,将调用onRecieve()方法,以更改RINGING,IDLE和OFFHOOK。 However, there are some occasion when the method is NOT called for RINGING (when a call is incoming) but it is still called for IDLE (when I hang up the call). 但是,在某些情况下,该方法不是为RINGING调用的(来电时),而是为IDLE调用的(我挂断电话时)。

From the manifest: 从清单中:

    <receiver android:name=".CallReceiver">
        <intent-filter android:priority="999">
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

The onReceive method itself is pretty simple: onReceive方法本身非常简单:

public void onReceive(Context voCtx, Intent voIntent)
{
    Log(voCtx, "onReceive: " + voIntent.getAction());
    if(voIntent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED))
    {
        HandleCallStateChanged(voCtx, voIntent);
    }
}

Log just outputs to my log file, so I can see what's happening. 日志仅输出到我的日志文件中,因此我可以看到发生了什么。 I'm not sure why onReceive() is only called sometimes. 我不确定为什么有时只调用onReceive() Could some other app be intercepting the broadcasts and cancelling them before my app gets them? 在我的应用获取广播之前,其他某个应用可以拦截广播并取消广播吗?

Android now has a feature of Doze and App standby. Android现在具有“打ze”和“ App待机”功能。 So, if your app enters this mode, then your BroadcastReceiver will not trigger. 因此,如果您的应用进入此模式,那么您的BroadcastReceiver将不会触发。

To allow your receiver to get triggered you can make use of REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission . 为了让您的接收者被触发,您可以使用REQUEST_IGNORE_BATTERY_OPTIMIZATIONS权限。 This way your app will be excluded from the battery optimisation process. 这样,您的应用将被排除在电池优化过程之外。

CAUTION: GOOGLE MAY BAN YOUR APP IF YOU DON'T USE THIS PERMISSION WITH CARE 注意:如果您不小心使用此权限,Google可能会禁止您的应用程序

It looks like this is specific to Samsung phones. 看来这是特定于Samsung手机的。 Under the battery settings, there is an "App power monitor" (in addition to the normal Battery Optimization stuff). 在电池设置下,有一个“应用程序电源监视器”(除了正常的电池优化功能外)。 This App power monitor can put apps to "sleep" (which is different from App Standby). 此应用程序电源监视器可以使应用程序进入“睡眠”状态(与“应用程序待机”不同)。 Setting my app as an "Unmonitored app" seems to have fixed my problem. 将我的应用设置为“不受监视的应用”似乎已经解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM