使用服务类内的AsyncTask的? [英] Using an AsyncTask inside a Service Class?
本文介绍了使用服务类内的AsyncTask的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要的数据上传到服务器。我使用的是在同一个进程,我的应用程序运行的服务。我应该使用一个单独的线程上传过程中还是应该使用的AsyncTask将数据上传到服务器?
I have to upload data to a server. I am using a service that is running on the same process as my application. Should I use a Separate thread for upload process or Should I use a AsyncTask to upload data to server ?
更具体地说我可以使用AsyncTask的一个服务类里面呢?我应该使用它?这个服务应该总是在存储器以数据发送到服务器,每5秒运行
More specifically can I use AsyncTask inside a service class ? And should I use it ? This service should always be running in memory in order to send data to the server every 5 seconds.
推荐答案
是的,你可以,下面的code将运行每5秒。使用常规连接code发送的一部分。
Yes you can, the below code will run every 5 seconds. Use your regular connection code for sending part.
public class AsyncTaskInServiceService extends Service{
public AsyncTaskInServiceService () {
super("AsyncTaskInServiceService ");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
final Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//Connect to database here
try {
} catch (JSONException e) {
e.printStackTrace();
}
}
}, 0, 5000);
}
}
这篇关于使用服务类内的AsyncTask的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文