Using an AsyncTask inside a Service Class?

后端 未结 3 534
闹比i
闹比i 2021-01-06 02:29

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

3条回答
  •  再見小時候
    2021-01-06 03:00

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

提交回复
热议问题