您好,我已经写了提醒代码在服务onstart().and当用户插入日期-时间和插入记录时服务调用startservice(),但只有服务是启动当我插入记录时,即我得到提醒当它从我的activity.but调用我想要提醒后3天或其他什么,所以我应该如何保持服务始终打开,以便我可以在未来得到提醒?或者我应该如何让服务的连接保持活动状态?我应该从我的任何活动中调用bindservice()函数吗?提前感谢
发布于 2011-11-16 19:50:54
首先不需要服务,您可以使用AlarManagerClass Link警报管理器类来安排事件,并在特定的时间和日期显示警报。如果你想在一段较长的持续时间后显示消息,那么通过AlarmManager调度一个挂起的意图,在相关的时间点启动服务来完成它的工作。完成后,按照上面的答案再次终止服务。此外,您还可以将数据永久存储到共享首选项中。您可以随时检索它,以便在设备重新启动时重新刷新它或用于其他目的。
发布于 2011-11-16 18:52:29
不要让你的服务一直在运行。它会在不必要的时候消耗电池和内存。而是通过AlarmManager调度一个PendingIntent,在相关的时间点启动服务来完成它的工作。完成后,再次终止该服务。
一般来说,android服务的使用与“普通”计算机上的服务/守护进程不同。它们执行一个任务,然后退出(通常通过Service.stopSelf()__),直到有人再次启动它们来做更多的工作。
下面是一个如何使用AlarmManager的小示例:
// get a calendar with the current time
Calendar cal = Calendar.getInstance();
// add 15 minutes to the calendar object
cal.add(Calendar.MINUTE, 15);
Intent intent = new Intent(ctx, YourService.class);
PendingIntent pi = PendingIntent.getService(this, 123, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);这将在15分钟后启动YourService的意图。通过这种方式发送意图的文档很多,请搜索一下。
²这最终会让你的用户感到沮丧:“为什么这个应用会浪费我的电池?”是一个很常见的问题
发布于 2011-11-16 19:04:24
有时,您的android应用程序可能需要在未来某个时间完成任务。为此,您必须安排一个使用安卓AlarmManager运行的活动(也可以是一个服务)。这篇文章将会显示:
* How to set up a receiver for the scheduled event
* How to create an activity from this receiver
* Using the AlarmManager and the created classes to successfully receive and process a scheduled event创建BroadcastReceiver
您需要的第一件事是接收事件的接收器。要使接收器正常工作,有几个关键方面。首先,创建一个扩展BroadcastReceiver的类,覆盖并实现必要的onReceive(Context context,intent)方法。以下是使用Toast消息的基本示例:
package com.justcallmebrian.alarmexample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import android.os.Bundle;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}在前面的示例中,我们只是简单地打印出由传入的字符串提供的消息,该字符串的名称为alarm_message。对于那些不熟悉Toast的人来说,它基本上是提供给用户的一条简短而快速的消息。在这里你可以找到更多关于Toast的信息。
除了实际创建接收事件的类之外,还必须在AndroidManifest.xml中声明它。下面是应该在结束应用程序标记之前添加的行(在此之前)。
基本上,这说明类AlarmReceiver是可用的,并将启动一个私有进程。一旦完成,您的BroadcastReceiver就可以运行了。
使用AlarmManager设置事件为了接收事件,您显然必须安排事件。安排事件有三种方法(使用set方法的一次性事件,使用setRepeating方法的重复事件,最后使用setInexactRepeating)。本教程将介绍使用set方法的一次性警报。有关其他事件的更多信息,请查看AlarmManager。
下面的代码片段将获取AlarmManager并将事件设置为从当前时间起5分钟后发生:
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 5);
Intent intent = new Intent(ctx, AlarmReceiver.class);
intent.putExtra("alarm_message", "O'Doyle Rules!");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);这段代码基本上获取了一个新的Calendar对象,并将其增加了5分钟。意图是用我们之前创建的AlarmReceiver创建的。代码中更重要的部分是设置FLAG_UPDATE_CURRENT标志。如果没有此标志,作为额外消息传递的消息将会丢失,接收方也无法获取。
使用这些代码片段,您应该能够在BroadcastReceiver中创建和运行一些任务。但是,有时您可能希望在报警事件上启动新的活动(或服务)。为此,您需要让AlarmReceiver创建并启动新的活动。
从BroadcastReceiver启动活动在接收器内启动活动需要一个额外的标志。我们将更改之前AlarmReceiver的onReceive来完成此操作:
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Intent newIntent = new Intent(context, AlarmActivity.class);
newIntent.putExtra("alarm_message", message);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}现在,您只需像创建任何其他活动一样创建新的AlarmActivity。不要忘记在AndroidManifest.xml文件中包含新创建的活动。
https://stackoverflow.com/questions/8150405
复制相似问题