2014-01-08 71 views
0

我需要在每次打開或執行應用程序時執行異步任務,因爲我使用Asyntask從http和每次應用程序執行中獲取一些json數據必須是新數據。強制asynctask在每次打開應用程序時運行

任何想法如何強制這個?

感謝

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    etResponse = (TextView)findViewById(R.id.etResponse); 
    etdia = (TextView)findViewById(R.id.dia); 
    etmes = (TextView)findViewById(R.id.mes); 

    // check if you are connected or not 
    if(isConnected()){ 

    } 

    new HttpAsyncTask().execute("URL TO EXECUTE"); 
} 

回答

3

放置execute方法在onResume()方法。

@Override 
    protected void onResume() { 
     super.onResume(); 
     new HttpAsyncTask().execute("URL TO EXECUTE"); 
    } 
+0

感謝克萊它的工作:-) – David

1

我看到你解決了你的問題。無論如何,我會把這裏的方法Exec中的AsyncTask的應用程序的每一個活動:

public class myActivity extends Activity { 

    private String mLastUrl = ""; 

    public void execAsync(String url) { 
     new HttpAsyncTask().execute(url); 
     mLastUrl = url; 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     execAsync(mLastUrl); 
    } 

    //your asynctaskcode 

在所有你想運行的AsyncTask只是這樣做的其他活動:

public class activityName extends myActivity { 
+0

這應該是一條評論 – Emmanuel

+0

我只是更新答案,如果我收到答覆! – iGio90

+0

每次活動開始,克萊解答感謝您的消息iGio90 – David

相關問題