我在应用程序中设置警报时遇到一致性问题,其中警报有时会被传递,但有时候未处理的意图不会传递给我的应用程序。
我尝试测试的方法是更改本地设备时间并检查是否收到警报。
我可以看到警报管理器触发警报,因为此日志出现
V AlarmManager: Triggering alarm #0: 0 when =1508253750804 package=com.x.xoperation =*walarm*:com.x.x/x.AlarmReceiver
android是否根据应用程序是否在后台控制警报的传递?任何有关该领域的见解都会有所帮助。
AlarmManager代码
public static void SetNotification(int id, long delayMs, String title, String message, String ticker, int sound, int vibrate,
int lights, String largeIconResource, String smallIconResource, int bgColor, String bundle, String dataString)
{
Context currentActivity = UnityPlayer.currentActivity;
AlarmManager am = (AlarmManager)currentActivity.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(currentActivity, AlarmReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("ticker", ticker);
intent.putExtra("title", title);
String arr[] = new String[]{id+"", delayMs+"", title, message, ticker, sound+""};
String fStr = "";
for (String temp:arr
) {
fStr += temp+":";
}
Log.d(Constants.TAG, "Input message = "+fStr);
intent.putExtra("message", message);
intent.putExtra("id", id);
intent.putExtra("color", bgColor);
intent.putExtra("sound", sound == 1);
intent.putExtra("vibrate", vibrate == 1);
intent.putExtra("lights", lights == 1);
intent.putExtra("l_icon", largeIconResource);
intent.putExtra("s_icon", smallIconResource);
intent.putExtra("bundle", bundle);
intent.putExtra("dataString", dataString);
PendingIntent pendingIntent = PendingIntent.getBroadcast(currentActivity,
id, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pendingIntent);
long finalTime = System.currentTimeMillis() + delayMs;
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,finalTime, pendingIntent);
Log.d(Constants.TAG, "event fired finalTime = "+ finalTime);
}
这是因为android引入了打瞌睡和app待机吗?或者是因为我们方面的一些问题。