我需要通过在指定的时间间隔(如5分钟)之后发送呼叫来从服务器获取数据。所以app会在5分钟后继续检查新数据。它就像gmail或facebook。这会在一段时间后自动获取新的订阅源或电子邮件并显示在列表中。我正在使用以下服务:
public class MessagesLoaderService extends Service {
// constant
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;
//********************************************************************************************************************************/
@Override
public IBinder onBind(Intent intent) {
return null;
}
//********************************************************************************************************************************/
@Override
public void onCreate() {
// cancel if already existed
if (mTimer != null)
{
mTimer.cancel();
}
else
{
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new MessageLoaderTask(), 0, Commons.TIME_INTERVAL_REFRESH_MESSAGES);
}
//********************************************************************************************************************************/
class MessageLoaderTask extends TimerTask
{
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable()
{
@Override
public void run() {
//Get Data from Server and store in local db
}
});
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Utils.showToast_msg(getApplicationContext(), "Service Destroyed");
}
//********************************************************************************************************************************/
}
/////////////////////////////////////////////// ///////////////////
从主要活动MainActivity启动服务:
startService(new Intent(this, MessagesLoaderService.class));
我希望服务只在应用程序运行时或在前台/后台运行(5分钟后发送呼叫)。但问题是即使我退出应用程序它也会继续运行。我希望服务在应用程序关闭时停止。对此有何解决方案?
答案 0 :(得分:1)
如果您希望停止使用mTimer.cancel(),请不要在“OnDestroy”中停止计时器?
答案 1 :(得分:1)
当您输入实际查询服务器的活动时,此方法有效。在onCreate中调用该方法。如果返回的值为true,则从服务器获取数据,如果为false,则执行youf流中的任何操作。
下面的示例使用Singleton类。当前系统时间加上五分钟存储在单例类变量中,而局部变量存储当前时间。如果当前时间超过Singleton变量的时间,则返回true并且是时候调用服务器。
SingletonClass app;
app = (SingletonClass ) getApplication();
public boolean serverQueryFrequency() {
boolean isTimeElapsed;
Calendar cal = Calendar.getInstance();
long time = cal.getTimeInMillis();
// If No Time is set, only then Set the Current time + 10 into
// application variable. This should fire only once, until 10 minutes
// have passed
if (app.getServerCallTime() == 0) {
Calendar cal2 = Calendar.getInstance();
// updating calendar to get current time + 10
cal2.add(Calendar.MINUTE, 5);
long timeTen = cal2.getTimeInMillis();
app.setServerCallTime(timeTen);
// returning true, to enable server check
return true;
}
// Log.v("******", "Current : " + time);
// Log.v("******", "App Time : " + app.getServerCallTime());
// Comparing current time with SeverCalltime which is set 10 minutes
// ahead. Code below fires conditionally as stated
if (time == app.getServerCallTime() || time > app.getServerCallTime()) {
isTimeElapsed = true;
// Once true fired from here, reset serverCallTime
app.setServerCallTime(0);
} else {
// 5 minutes have not passed
isTimeElapsed = false;
}
// returning the related value
return isTimeElapsed;
}
答案 2 :(得分:0)
您可以使用此行停止服务
stopService(new Intent(this, MessagesLoaderService.class));
所以你的服务停止了
你需要在你的应用程序中识别退出应用程序的位置,此时你需要调用上面的代码,操作系统会在某些情况下自动终止服务,如电量不足等等但是这不是很好的解决方案,所以你可以停止它在你申请退出点的上面一行
答案 3 :(得分:0)
我已经知道应用程序何时关闭服务也因为它们在一个线程中而关闭,所以服务应该在另一个线程上,以便它不被关闭,查看并考虑保持服务这里以警报管理器为例http://www.vogella.com/articles/AndroidServices/article.html,这样您的服务就不会显示在通知中。
最后,经过我所做的所有研究后,我才意识到长时间运行服务的最佳用途是启动前景();因为它是为此制作的,系统实际上可以很好地处理您的服务。
答案 4 :(得分:0)
当用户按下应用程序第一页上的后退按钮时,表示他们想要。 覆盖onbackpressed并将stopService调用放在那里。 其他.. 使用一个退出按钮..给它一个onclick并在里面把stopService放在那里