每5分钟后开始的Andr​​oid服务 [英] Start Android Service after every 5 minutes

查看:198
本文介绍了每5分钟后开始的Andr​​oid服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在互联网上的最后2天,但我找不到任何教程有帮助。我创建了一个服务,我送一个通知状态栏在服务启动时。我想该服务停止显示该通知后,5分钟后再次启动它。请让我知道,如果有可能,如果您有任何给我提供了一些有用的教程。听说的TimerTask AlarmManager 中,我试图把它们作为很好,但我没能得到想要的的结果。

I was searching over the internet for last 2 days but I couldn't find any tutorial helpful. I have created a service and I am sending a notification in status bar when the service starts. I want that service to stop after showing the notification and start it again after 5 minutes. Please let me know if it is possible and provide me some helpful tutorials if you have any. I heard of TimerTask and AlarmManager and I tried to use them as well but I wasn't able to get the desired result.

编辑:我需要的服务启动,即使我的应用程序没有运行每5分钟

I need the service to be started every 5 minutes even if my application is not running.

推荐答案

您不想使用的TimerTask ,因为这依赖于持续运行你的应用程序。一个 AlarmManager 的实施使得它的安全为您的应用程序执行之间被杀害。

You do not want to use a TimerTask since this depends on your application running continuously. An AlarmManager implementation makes it safe for your application to be killed between executions.

指出您尝试使用 AlarmManager ,但并没有得到想要的结果不是一个有用的声明,因为它告诉任何人怎么帮你得到它的权利。这将是更有益的EX preSS发生了什么事。

Stating that you tried to use AlarmManager but did not get the desired result is not a helpful statement, in that it tells no one how to help you to get it right. It would be much more useful to express what happened.

<一个href="http://$c$c4reference.com/2012/07/tutorial-on-android-alarmmanager/">http://$c$c4reference.com/2012/07/tutorial-on-android-alarmmanager/包含这似乎是对 AlarmManager 一个有用的教程。以下是要点:

http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/ contains what appears to be a useful tutorial on AlarmManager. Here are the salient points:

1)你的报警器会导致意图来到期时触发。它是由你来决定什么样的的意图以及应如何实施。我提供的链接有基于一个BroadcastReceiver一个完整的例子。

1) Your alarm will cause an Intent to fire when it expires. It's up to you to decide what kind of Intent and how it should be implemented. The link I provided has a complete example based on a BroadcastReceiver.

2)你可以用一个例子,如安装报警器

2) You can install your alarm with an example such as:

public void setOnetimeTimer(Context context) {
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.TRUE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 60 * 5), pi);
}

这篇关于每5分钟后开始的Andr​​oid服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆