如何让我的应用程序服务始终在android2.1中运行?

时间:2011-11-16 10:47:46

标签: android android-service reminders

您好我已经在服务onstart()中写了提醒代码。当用户插入日期时间并插入由startservice()函数调用的服务时的记录,但只有服务在我插入记录时启动,即我正在收到提醒当它从我的活动中接到电话。但是我想在3天之后提醒或者其他什么,所以我应该如何保持服务,以便我将来可以得到提醒?或者我应该如何使服务连接保持活动?我应该从我的任何活动调用bindservice()frunction或者什么? 提前谢谢---

3 个答案:

答案 0 :(得分:2)

不要让您的服务一直运行。它不需要时消耗电池和内存¹。 而是通过AlarmManager安排PendingIntent在相关时间点开始服务,以便完成工作。完成后,再次终止服务。

在一般情况下,机器人服务与“普通”计算机上的服务/守护程序不同。他们有一个他们执行的任务,然后他们退出(通常是通过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的意图。有很多文档可以用这种方式发送意图,搜索一下。

¹这最终会让用户感到沮丧:“为什么这个应用会浪费我的电池?”是一个非常常见的问题

答案 1 :(得分:0)

有时,您的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

您需要的第一件事是接收事件的接收器。接收器正常工作有几个关键方面。首先创建一个扩展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名称传入的String提供的消息。对于那些不熟悉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启动活动 在Receiver中启动活动会有一个额外的标志。我们将更改之前的onReceive for 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文件中包含新创建的活动。

答案 2 :(得分:0)

首先不需要服务,您可以使用AlarManagerClass Link Alarmanger类来安排活动并在特定时间和日期显示警报。如果您想在一段很长的时间之后显示消息,那么通过AlarmManager安排Pending-intent,在相关的时间点启动服务以完成它的工作。完成后,按照上述答案告诉我再次终止服务。此外,您可以将数据永久存储到共享首选项中。您可以随时检索它,以便在设备重启时进行重新设置或用于其他目的。