Android - Tamagotchi 游戏后台服务

Android - Tamagotchi game background service

我尝试使用 Unity 为 Android 构建一个 Tamagotchi 游戏。 Tamagotchi 的食物属性即使在游戏关闭时也会下降。

所以我的方法是构建一个 IntentService 来保存变量,并在需要时增加和减少它们。出于电池原因,如果应用程序关闭,我会使用警报管理器每 10 分钟启动一次服务。如果应用程序启动,它将绑定服务,因此它永远不会关闭,我可以获得变量。

有没有更有效的方法来处理这个问题?我不希望我的应用耗电太多。

€: 如果食物变少,它也应该显示一个通知。

确实有更有效的方法来处理这个问题。

为什么不在用户关闭应用时保存一个时间戳,并计算用户再次打开应用的时间? 然后你可以计算你完成的新食物属性。

编辑:如果你想在食物不足时显示通知,你需要警报管理器。

下面是一些示例代码:

public static void registerAlarm(Context context) {
Intent i = new Intent(context, YOURBROADCASTRECIEVER.class);

PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE, i, 0);

// We want the alarm to go off 3 seconds from now.
long startTime = SystemClock.elapsedRealtime();
startTime += 60000;//start 1 minute after first register.

// Schedule the alarm!
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, startTime, 900000, sender); // 15min interval

}

您还可以在游戏结束前计算电子宠物什么时候会饿,并只在该时间点设置警报以显示通知。除非游戏再次打开,因此您清除所有通知。