【问题标题】:Should I use AsyncTask or IntentService to update Dates by URL Once in a day?我应该使用 AsyncTask 还是 IntentService 来通过 URL 一天更新一次日期?
【发布时间】:2015-01-19 09:22:20
【问题描述】:

在我的应用程序中,我使用 AsyncTask 从 URL 获取数据。通过这个 URL,我每天都用一些新数据更新我的应用程序。
但是,我想知道哪个最适合我 AsyncTaskIntentService 通过 URL 更新我的数据并每天下载一次?

【问题讨论】:

  • 创建服务,实现在一天内运行的 AlarmManager
  • @ Murtaza Hussain .你有任何演示代码...???
  • 我将其发布为答案,希望对您有所帮助

标签: android android-asynctask android-service


【解决方案1】:

假设您的 intentService 已启动并正在运行

每天调用一次您的 intentService

  Intent myIntent = new Intent(context, MyServiceReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context,  0, myIntent, 0);

  AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(System.currentTimeMillis());
  calendar.add(Calendar.SECOND, 60); // first time
  long frequency= 86400000 * 1000; // every 24 hours
  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), frequency, pendingIntent);    

【讨论】:

    【解决方案2】:

    查看此站点以比较服务、线程、IntentServices 和 AsyncTasks

    但我个人认为你应该使用AlarmManager

    【讨论】:

      猜你喜欢
      • 2013-02-16
      • 2013-06-06
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      相关资源
      最近更新 更多