简体   繁体   中英

Should I use AsyncTask or IntentService to update Dates by URL Once in a day?

In my app I use AsyncTask to get datas from an URL. By this URL, I update my app everyday with some new data.
However, I'm wondering which is the best for me AsyncTask or IntentService to update my datas by URL and download it once in a day?

Assuming you have intentService UP and Running

call your intentService for once a day

  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);    

Check this site for a comparison between Services, Threads, IntentServices and AsyncTasks .

But personally I think you should use an AlarmManager .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM