嗨,我已经在服务 onstart() 中编写了提醒代码。当用户插入日期时间并插入记录时,服务由 startservice() 函数调用,但是当我插入记录时只有服务正在启动,即当它得到提醒时从我的活动中打来电话。但我想在 3 天后收到提醒,所以我应该如何保持服务始终开启,以便将来能收到提醒?或者我应该如何使服务连接保持活动状态?我应该从我的任何活动中调用 bindservice() 函数还是什么?提前致谢 - -
3 回答
不要让您的服务一直运行。不需要时会消耗电池和内存¹。而是PendingIntent
通过AlarmManager安排在相关时间点启动服务以完成工作。完成后,再次终止服务。
一般来说,机器人服务的使用与“普通”计算机上的服务/守护进程不同。他们有一个任务要执行,然后他们退出(通常通过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);
YourService
这启动了从现在起 15 分钟内开始的意图。有很多以这种方式发送意图的文档,搜索一下。
¹这最终会让您的用户感到沮丧:“为什么这个应用程序会浪费我的电池?” 是一个很常见的问题
有时,您的 android 应用程序可能需要在未来某个时间完成任务。为此,您必须使用 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 的类并覆盖并实现必要的 onReceive(Context context, Intent 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 创建一个 Intent。代码中更重要的部分是设置 FLAG_UPDATE_CURRENT 标志。如果没有这个标志,作为 Extra 传递的消息将丢失并且不会被接收者获取。
使用这些代码片段,您应该能够在 BroadcastReceiver 中创建和运行一些任务。但是,有时您可能希望在警报事件上启动新的活动(或服务)。为此,您需要让 AlarmReceiver 创建并启动新的 Activity。
从 BroadcastReceiver 启动 Activity 在 Receiver 中启动 Activity 需要一个额外的标志。我们将更改以前的 onReceive 为 AlarmReceiver 以完成此操作:
@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();
}
}
现在您只需像创建任何其他 Activity 一样创建新的 AlarmActivity。不要忘记在 AndroidManifest.xml 文件中包含新创建的活动。
首先不需要服务,您可以使用AlarManagerClass Link Alarmanger 类来安排事件并在特定时间和日期显示警报。如果您想在一段时间后显示消息,那么通过 AlarmManager 安排一个待处理的意图,这会在相关时间点启动服务来完成它的工作。完成后,按照上述答案再次终止服务。此外,您可以将您的数据永久存储到共享首选项中。您可以随时检索它,以便在设备重新启动时重新安装它或用于其他目的。