我需要在一段时间后重启我的机器人。我的应用程序是带有图像的幻灯片。用户通过主页按钮或后退按钮关闭应用程序。我需要在一段时间后重启应用程序(丢失用户触摸设备后几分钟 - 就像屏幕保护程序一样)。是否有可能做到这一点? 任何人都可以帮我,我该怎么做?
答案 0 :(得分:0)
我不确定这是个好主意。如果您的用户想要退出您的应用,为什么要将它带到前面?
但是,我认为您可以使用background service来实现这一目标,Intent会在一段时间后启动{{3}}。
答案 1 :(得分:0)
Imho最好的解决方案是Alarm Manager。 http://developer.android.com/reference/android/app/AlarmManager.html 样品:
long nexttime = (new Date()).getTime() + 60L*60L*1000L;//one hour after.
AlarmManager am=(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
Intent intent = new Intent(this, MyService.class);
intent.putExtra(SomeExtras, false);//for instance
PendingIntent pi = PendingIntent.getService(this, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC,nexttime, pi);