服务做定期工作

发布于 2024-12-10 14:45:42 字数 123 浏览 1 评论 0原文

我正在开发一个 Android 应用程序,它必须在后台执行 2 个定期任务:

  1. 每 24 小时从服务器下载文件。
  2. 每周对手机SD卡进行文件操作。

我该怎么做?

I am developing an Android app which must perform 2 periodic tasks in background:

  1. download files from server every 24 hours.
  2. perform file operations each week on phone sd card.

How do i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情换悲伤 2024-12-17 14:45:42

首先您需要使用AlarmManager。当注册的警报(例如24小时情况)将触发时,您可以从AlarmManager的广播接收器调用服务。如果您还不了解 AlarmManager,则需要研究一些有关 AlarmManager 的知识。如需进一步帮助,您可以从下面的代码中获得一些想法:

Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + 1); //+1 For Next day (24 hours or so...)
cal.set(Calendar.HOUR_OF_DAY,  hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

AlarmManager am = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
am.cancel(pendingIntent);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

以下是设置闹钟的方法。现在,当 Alarm 被触发时,您将像这样调用您的服务:

public class AlarmReceiver extends BroadcastReceiver
{   
   @Override
   public void onReceive(Context arg0, Intent arg1)
   {    
           // Call you service or any task here
   }
}

最后一件事,不要忘记在 AndroidManifest.xml 中提及您的广播接收器和服务

<receiver android:name=".AlarmReceiver">
</receiver>    
<service android:name=".MyService"/> 

Firstly you need to use AlarmManager. When the registered alarm e.g. 24 hours case, will trigger, you can call service from the broadcast receiver of AlarmManager. You need to study a bit about AlarmManager if you don't already know. For further help you can get some idea from code below:

Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + 1); //+1 For Next day (24 hours or so...)
cal.set(Calendar.HOUR_OF_DAY,  hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

AlarmManager am = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
am.cancel(pendingIntent);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

Here is how you can set your Alarm. Now when Alarm will be triggered, you will call your service like this:

public class AlarmReceiver extends BroadcastReceiver
{   
   @Override
   public void onReceive(Context arg0, Intent arg1)
   {    
           // Call you service or any task here
   }
}

Last thing, don't forget to mention your broadcast receiver and service in AndroidManifest.xml

<receiver android:name=".AlarmReceiver">
</receiver>    
<service android:name=".MyService"/> 
暗喜 2024-12-17 14:45:42

以下是官方 android 文档关于 AlarmManager

Note: The Alarm Manager is intended for cases where you want to have 
your application code run at a specific time, even if your application 
is not currently running. For normal timing operations (ticks, timeouts, etc) 
it is easier and much more efficient to use Handler.

Handler 文档。

Here's what official android docs says about AlarmManager

Note: The Alarm Manager is intended for cases where you want to have 
your application code run at a specific time, even if your application 
is not currently running. For normal timing operations (ticks, timeouts, etc) 
it is easier and much more efficient to use Handler.

Handler documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文