简体   繁体   English

一段时间后重新启动android应用程序

[英]Restart android application after a time

I need to restart my android after a time. 一段时间后,我需要重新启动android。 My application is a slideshow with images. 我的应用程序是带有图像的幻灯片。 The user close the application by home button or by back button. 用户通过主页按钮或后退按钮关闭应用程序。 I need to restart the application after a time (a few minutes after the lost user's touch on the device - like the screen ssaver works). 我需要一段时间后重新启动应用程序(丢失的用户触摸设备后几分钟-就像屏幕保护程序一样)。 Is it possible to do this? 是否有可能做到这一点? Can anyone help me how can I do this? 谁能帮助我该怎么做?

I'm not sure it's a very good idea. 我不确定这是个好主意。 If your user wants to quit your app, why should you bring it to the front? 如果您的用户想退出您的应用程序,为什么要把它放在前面呢?

However, I think you can achieve that using a background service , which launch an Intent after a certain time. 但是,我认为您可以使用后台服务来实现该目的 ,该服务会在一段时间后启动Intent

Imho the best solution is Alarm Manager. 恕我直言,最好的解决方案是警报管理器。 http://developer.android.com/reference/android/app/AlarmManager.html sample: 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);

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

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