AsyncTask之后的Android刷新服务

时间:2014-09-17 04:37:08

标签: android android-asynctask refresh page-refresh

我正在尝试在调用我的活动中的AsyncTask时重新启动下载和uppdate服务的整个功能,但我不确定我是否做错了什么。目前,我有一个启动AsyncTask的刷新按钮:

启动AsyncTask的刷新功能:

    final ImageView refreshBtn= (ImageView) findViewById(R.id.spin_refresh);
    Log.i("RootActivity:setupHeader","******ImageView refreshBtn******");
    //Listening to Button Click by User
    refreshBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.i("RootActivity:setupHeader","******OnClick,Call RefreshTask******");
            //CALL ON THE CLASS OF REFRESHTASK ASYNCTASK
            RefreshTask refreshtask = new RefreshTask();
            refreshtask.execute();
        }
    });

调用刷新函数后,它将调用我的活动中的refreshtask:

public class RefreshTask extends AsyncTask <Void,Integer,Long> implements Observer{
// SET THE PARAMETERS FOR REFRESHTASK IN ASYNCTASK: PRE-EXECUTION, PROGRESS UPDATES & POST-EXECUTION    

private Context context=null;
private DownloadService callback =null;
protected boolean exitAsync = false;
public static Hashtable<String, Download> downloadList;
final public static String PATH = "/sdcard/DCIM/100MEDIA/";
private static final Long START_STICKY = null;
Intent intent;
ListView lv;
RelativeLayout rl;
protected String dSResponse = null;

 public void UIThreadProgress(Context context, DownloadService callback) {
        this.context = context;
        this.DownloadService = DownloadService;
    }
//PROCESS IN CALLING THE DOWNLIST WHILE IN BACKGROUND
protected Long doInBackground(Void...params){

    // TODO Auto-generated method stub
    Log.i("----------RefreshAsyncTask----------", "doInBackground");

    try {
        // ====================DownloadService
        Thread.sleep(10000);
        while (this.callback.condition())
        Intent i= new Intent(context.this, UpdateService.class);
        Intent I= new Intent(context.this,DownloadService.class);
        // potentially add data to the intent
        context.this.startService(i); 
        } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }while (!exitAsync);    
    return START_STICKY;
}

@Override
public void update(Observable observable, Object data) {
    // TODO Auto-generated method stub
    //UpdateServiceStatus
}

//HANDLES EVENTS ON THE UI-POST-EXECUTE
@Override
protected void onPostExecute(Long result){
    super.onPostExecute(result);
    lv.findViewById(R.id.spin_refresh).setVisibility(View.VISIBLE);
    Log.i("----------RefreshAsyncTask----------", "onPostExecute");
    //GIVE FEEDBACK ON THE USER INTERFACE
    Toast.makeText(context, "Your document has been refreshed and contains the updated documents!", Toast.LENGTH_LONG).show();
    // After the process in doInBackground has been completed   
}

}

请帮助,我正在尝试在调用AsyncTask时重新实例化下载和更新服务。

2 个答案:

答案 0 :(得分:0)

我无法理解为什么你在一个线程内启动服务。您应该在服务中启动一个线程。在此链接中查看本地服务示例:

http://developer.android.com/reference/android/app/Service.html

在您的onClickListener解压缩服务中再次绑定它。它会刷新您的服务。在onCreate服务方法中,开始下载或更新线程。

注意:最好使用其他线程机制进行网络服务。 AsyncTask适用于短期操作。

答案 1 :(得分:0)

 @Override
 protected void onPostExecute(Long result){
super.onPostExecute(result);
lv.findViewById(R.id.spin_refresh).setVisibility(View.VISIBLE);
Log.i("----------RefreshAsyncTask----------", "onPostExecute");
//GIVE FEEDBACK ON THE USER INTERFACE
Toast.makeText(context, "Your document has been refreshed and contains the updated       documents!", Toast.LENGTH_LONG).show();

RefreshTask refreshtask = new RefreshTask();
        refreshtask.execute();
// After the process in doInBackground has been completed   
}