2016-02-04 58 views
0

我正在嘗試使用asynctask來持續更新我的主界面中的textview。還有另一個按鈕可以打開另一個活動。當我啓動應用程序時,asyntask啓動並向textview顯示數據,但是當我點擊另一個按鈕打開另一個活動並單擊返回按鈕返回aysnctask正在運行的上一個活動時,它停止向TextView中。當活動重新打開時,Asynctask停止在textView中顯示數據

你能幫助mem解決這個問題或者建議其他方法嗎?謝謝。

代碼MainActivity:

Button about; 
TextView tempData; 
Context context = this; 
AllSensorData sensorData; 


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

    about=(Button)findViewById(R.id.hAbout); 

    tempData=(TextView)findViewById(R.id.tempData); 

    sensorData=new AllSensorData(tempData,getApplicationContext()); 
    sensorData.execute("http://192.168.1.177/"); 


    about.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Intent i =new Intent(getApplicationContext(),AboutActivity.class);    
      startActivity(i); 
     } 
    }); 

代碼aynctask類:

public class AllSensorData extends AsyncTask<String, byte[], String>{ 

    TextView temp; 

    Context context; 

    public AllSensorData(TextView temp,Context context) { 
      this.temp=temp; 
      this.context=context; 
    } 


    InputStream nis; 
    OutputStream nos; 
    BufferedReader in; 
    DefaultHttpClient httpclient =new DefaultHttpClient(); 
    URL url; 
    URLConnection urlconn=null; 
    InputStreamReader isn; 

    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response; 
      try { 


       //this method is working for data only 
       while(true){//while connected 
      HttpGet httpget =new HttpGet("http://192.168.1.177/"); 
      response = httpclient.execute(httpget); 
      in=new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

       String msgFromServer = in.readLine();//read the lines coming from the socket 
       byte[] theByteArray = msgFromServer.getBytes();//store the bytes in an array 
       publishProgress(theByteArray);//update the publishProgress 


        if(isCancelled()){ 
         break; 
        } 
       } 


      } catch (ClientProtocolException e) { 

      } catch (IOException e) { 

      } 
     return null; 
    } 

    private boolean alreadyDisplayedNotification = false; 

    private boolean already =false ; 

    protected void onProgressUpdate(byte[]... values) { 
     super.onProgressUpdate(values); 
     String command=new String(values[0]);//get the String from the recieved bytes 
     String[] parts= command.split(","); 
     String part1=parts[0]; 


     temp.setText(part1); 

    } 
} 
+0

加入的onResume方法您的TextView更新代碼; – justDroid

+2

閱讀關於它的活動生命週期,並顯示以保存活動狀態 –

+0

我使用了onResumed方法,並且仍然當重新打開活動 – akshay270494

回答

0

通過這個save instance state去了解你的價值爲什麼文字未顯示

將您的「刷新」代碼放在onStart()而不是onResume()中。 onStart()獲取「在用戶看到活動變得可見時調用。」

經過Activity life cycle

+0

你的問題解決了嗎? @ akshay270494 –

+0

不,我不能使它顯示在textview中的數據。 – akshay270494

+0

好的。在我的答案你必須保存textview的價值,然後在活動OnRestart()再次設置@ akshay270494 –

相關問題