你好我已经写了服务onstart()提醒代码,当用户插入日期时间和插入记录在那个时候服务调用startservice()函数,但只有服务正在开始,当我插入记录即我得到提醒时它会从我的活动中接到电话,但我想要在三天之后提醒,所以我应该如何始终保持服务,以便将来可以提醒?或者我应该如何使服务连接保持活动?我应该从我的任何活动或什么调用bindservice()frunction? 在此先感谢---如何让我的应用程序服务始终在android2.1中运行?
回答
首先没有必要的服务,您可以使用在特定的时间和日期安排的活动和节目警报AlarManagerClass Link Alarmanger类。如果您想要在很长时间后显示消息,则可以通过在相关时间点启动服务的AlarmManager安排一个待定意图来执行此操作。完成后,按照以上答案的说明再次停用该服务。另外,您可以将数据永久存储到共享首选项中。您可以在任何时候检索它,以便在设备重新启动时重新安装或用于其他目的。
不要让您的服务始终运行。不需要时,它会消耗电池和内存¹。 宁可安排PendingIntent
通过AlarmManager即在相关时间点开始服务以完成工作。完成后,再次终止服务。
一般来说,androids服务在“普通”计算机上使用不同的服务/守护进程。他们有一个任务,他们执行,然后他们退出(通常通过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
你需要的第一件事就是接收器,接收该事件。接收器有几个关键方面可以正常工作。首先创建一个扩展BroadcastReceiver的类并覆盖并实现必要的onReceive(Context context,Intent intent)方法。下面是使用吐司消息一个基本的例子:
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的信息。
除了实际创建接收事件的类外,还必须在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);
这段代码基本上获得新的日历对象,并将5分钟吧。 Intent是使用我们之前创建的AlarmReceiver创建的。更重要的代码是设置FLAG_UPDATE_CURRENT标志。如果没有这个标志,那么作为附加信息传递的信息将会丢失,并且不会被接收器获得。
有了这些代码段,你应该能够在广播接收器来创建和运行一些任务。但是,有时您可能希望在警报事件中启动新的活动(或服务)。为此,您需要让AlarmReceiver创建并启动新的活动。
起价的BroadcastReceiver 启动接收器内的活动的活动具有被需要额外的标志。我们将改变以往的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();
}
}
现在你只需要创建新的AlarmActivity,你会做任何其他活动。不要忘记在AndroidManifest.xml文件中包含新创建的活动。
喜雅您的权利,但实际上我想去做发送短信的一些任务future.so为保持继续运行(),它的任务,我呼吁来自OnStart中发送短信的活动,也写在OnStart中这一任务的代码()。但是当我插入的记录和callng startservice提交按钮后,那么只有服务获取调用,但我想保持服务活着,生病需要的时候摧毁,所以我应该怎么办呢?我应该从活动绑定服务? – Sam
- 1. 如何让您的移动AIR应用程序始终在后台运行?
- 2. 如何确保我的服务始终处于运行状态
- 3. 如何让程序在Linux中作为服务运行
- 4. 让应用程序/服务“在屏幕关闭后运行”? -Android-
- 5. Python:始终运行程序
- 6. 按下按钮时,如何让我的应用程序运行终端代码?
- 7. 如何让我的应用程序在Windows Server 2008上运行?
- 8. 如何不让应用程序在任务运行时退出
- 9. 如何在iPhone运行应用程序中处理pushnotification服务
- 10. c# - 我如何使应用程序作为服务运行?
- 11. 不让我运行应用程序
- 12. 如何让android应用程序在后台运行,服务示例?
- 13. 如何让我的Rails 3.1应用程序在服务器上进行测试?
- 14. 如何在Ubuntu的终端中自动运行应用程序?
- 15. 如何让我的应用程序服务器CPU达到100%?
- 16. 如何在专用服务器上运行GAE应用程序?
- 17. 如何使Handler始终运行,但在java中关闭应用程序
- 18. 如何在Android2.1中将Google地图与我的应用程序集成?
- 19. Zuul服务器运行应用程序
- 20. 通过服务运行应用程序
- 21. 如何在服务器上运行Maven Java应用程序
- 22. 如何在xampp服务器上运行Angular应用程序?
- 23. 如何在tomcat服务器上运行spark应用程序
- 24. 如何在Apache Web服务器上运行ASP.NET应用程序?
- 25. 如何在关闭应用程序时保持服务运行?
- 26. 如何在Ubuntu终端中运行PyQt4应用程序?
- 27. 如何在Android应用程序中运行终端命令?
- 28. 应用程序始终在后台运行
- 29. 验证另一个应用程序始终在运行
- 30. 位置服务应用程序 - Xcode“停止”让位置服务永远运行
如何在完成当前服务任务后重新启动()服务,以便将来发送消息? – Sam
有可能从braodcast reciever对reboot.Check这个http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/ –
您可以再次绑定,然后启动服务启动服务。 –