简体   繁体   English

应用程序不在内存中时(One Plus 3)未收到警报管理器广播

[英]Alarm Manager broadcast not received when app is not in memory (One Plus 3)

Alarm Manager broadcast is not received in some devices when app is not in memory ( swiped off from app-tray ) . 当应用程序不在内存中(从应用程序托盘中滑出)时,在某些设备中未收到Alarm Manager广播。 Device in which it is not working runs with API 25 and is ONE PLUS 3 device. 无法使用的设备使用API​​ 25运行,并且是ONE PLUS 3设备。 however same thing works in Nexus 6 (with API 25). 但是在Nexus 6(使用API​​ 25)中也可以使用。

Strange thing is , I can see in system logs that Alarm is being triggered. 奇怪的是,我可以在系统日志中看到警报正在触发。 I can see log below when alarm is supposed to go off. 当警报响起时,我可以在下面看到日志。 Just that broadCast is not received. 只是没有收到broadCast。 again, this happens only when app is not in memory. 同样,仅当应用程序不在内存中时,才会发生这种情况。 Broadcast is received properly when app is in memory. 当应用程序在内存中时,可以正确接收广播。

V/AlarmManager: Triggering alarm #0: 0 when =1508843147121 package=net.IntAppoperation =*walarm*:com.intapp.receivers.NOTIFICATION_ALARM

Here is how I'm setting alarm 这是我设置闹钟的方式

In Manifest : 在清单中:

<receiver android:name="com.intapp.receivers.ReminderReceiver"
              android:exported="false"
              android:enabled="true">
        <intent-filter>
            <action android:name="com.intapp.receivers.NOTIFICATION_ALARM" />
        </intent-filter>
</receiver>

And then When I want to set alarm : 然后当我想设置警报时:

public static final String ACTION = "com.intapp.receivers.NOTIFICATION_ALARM";

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReminderReceiver.class);
intent.setAction(ACTION);
int randomNum = new Random().nextInt(Integer.MAX_VALUE - 1 + 1);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, randomNum, intent, PendingIntent.FLAG_ONE_SHOT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
     alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent); // I get time from calendar instance in my function
  } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
            }
}

Here's my implementation of Receiver : 这是我的Receiver的实现:

public class ReminderReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Receiver called ");
        // my implementation 
     }
}

EDIT: 编辑:

Seems like problem is a little bigger. 好像问题更大了。 I have also registered for CALL_STATE by which I can listen to incoming/outgoing calls. 我还注册了CALL_STATE ,可以通过它收听传入/传出的呼叫。 That broadcast receiver is also not firing when app is not in memory. 当应用程序不在内存中时,该广播接收器也不会触发。 Plus, This happens on Samsung device too running on API 25. Is this (Manifest broadcast not working) known problem for API 25 on some devices? 另外,这也发生在同样在API 25上运行的Samsung设备上。某些设备上的API 25是否已知此问题(清单广播不起作用)?

I have an OnePlus 3 as well... And yes, this happens a lot to me as well. 我也有一个OnePlus 3 ...是的,这对我也很常见。 Even when I register for a BootReceiver . 即使我注册了BootReceiver I fixed my problem by uninstalling and reinstalling my application. 我通过卸载并重新安装应用程序解决了问题。

I would suggest to implement a WakeLock in your Application , to prevent the device from killing your app, so the BroadcastReceiver remains active. 我建议在您的Application实现WakeLock ,以防止设备杀死您的应用程序,因此BroadcastReceiver保持活动状态。

Let's hope OnePlus will do it better in the future. 让我们希望OnePlus将来会做得更好。

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

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