在Android的后台调用多个服务 [英] Call multiple services in background in android

查看:190
本文介绍了在Android的后台调用多个服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要调用后台的多个服务在Android应用程序的项目。
 在每一个服务,我有打电话给单独的Web服务,并得到了一些数据,并与当地SQLite数据库处理。我能分别调用各服务并可以操纵它与本地数据库的结果。
但不能够调用所有服务的序列。
我的code是如下:

I have to call multiple services in background for a project in android app. In each services i have to call separate web service and get some data and process it with local sqlite database. I am able to call each service separately and can manipulate its result with local database. But not able to call all services in a sequence. my code is as below:

@Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Timer timer = new Timer();

        final SyncTableUsers syncUserObj = new SyncTableUsers(LocalService.this);
        final SyncTableVehicles syncVehicleObj = new SyncTableVehicles(this);
        final SyncTableLicenses syncLicenseObj = new SyncTableLicenses(this);
        final SyncTableTags syncTagObj = new SyncTableTags(this);
        final SyncTableTagMappings syncTagMapObj = new SyncTableTagMappings(this);
        final SyncTableAddresses syncAddressObj = new SyncTableAddresses(this);
        final SyncTableDispatches syncDispatchObj = new SyncTableDispatches(this);
        final SyncEditCompany syncCompanyObj = new SyncEditCompany(LocalService.this);
        final SyncEditVehicle syncEditVehicleObj = new SyncEditVehicle(LocalService.this);
        final SyncEditUser syncEditUserObj = new SyncEditUser(this);
        final SyncVehicleLog vLogObj = new SyncVehicleLog(LocalService.this);
        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                        syncUserObj.syncUserData();
                        syncVehicleObj.syncVehicleData();
                        syncLicenseObj.syncLicensesData();
                        syncTagObj.syncTagData();
                        syncTagMapObj.syncTagMappingData();
                        syncAddressObj.syncAddressData();
                        syncDispatchObj.syncDispatchData();
                        syncCompanyObj.syncCompanyData();
                        syncEditVehicleObj.syncVehicleData();
                        syncEditUserObj.syncUserData();
                        Log.i("TAG", "LogId After insert values ");
                }
            }
        };
        timer.scheduleAtFixedRate(timerTask, 10000, 180000);  call after every
                                                             3 minute
        super.onStart(intent, startid);

syncUserData是一种方法,其中调用web服务。

syncUserData is a method, which call web service.

推荐答案

我建议你去的AsyncTask的解决方案。它是利用具有最新的OS virsion设备上运行的请求或任何其他后台任务和调用web服务的一个简单易懂的方式,必须需要使用的AsyncTask

I recommend you go for the AsyncTask solution. It is an easy and straightforward way of running requests or any other background tasks and calling web services using devices having latest OS virsion you must need to use AsyncTask.

这也很容易实现如onProgressUpdate如果你需要显示某种形式的进度条在运行您的要求。

It's also easy to implement e.g. onProgressUpdate if you need to show a progress bar of some sort while running your requests.

private class YourTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
    //call your methods from here
         //publish yor progress here..
        publishProgress((int) ((i / (float) count) * 100));
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
    //action after execution success or not
 }
}

这篇关于在Android的后台调用多个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆