2013-04-26 93 views
-2

我按照這個計算器鏈接Auto refresh the activity它每5秒刷新一次活動我想刷新活動一次只創建一次不是每5秒鐘我該做什麼?請幫我如何刷新創建活動?如何刷新活動

  public void onCreate(Bundle savedInstanceState) 
    { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

    this.mHandler = new Handler(); 

    this.mHandler.postDelayed(m_Runnable,5000); 


    }//onCreate 

    private final Runnable m_Runnable = new Runnable() 
{ 
    public void run() 

{ 
    Toast.makeText(refresh.this,"in runnable",Toast.LENGTH_SHORT).show(); 

    refresh.this.mHandler.postDelayed(m_Runnable, 5000);    
} 

};//runnable 
+1

我真的不知道你刷新的意思。我也不知道你是否想要刷新5秒。在創建之後或者隨着創建的調用。 – xeed 2013-04-26 11:00:19

+0

我從意圖使用意圖從意圖Finisg此活動不刷新和顯示圖片,直到我回來並再次在此活動 – 2013-04-26 11:02:17

+0

因此,你的想法是實際上每五秒刷新一次嗎?不只一次? – xeed 2013-04-26 11:04:23

回答

0

既然你在評論中描述了你的實際問題,我會在這裏回答這個問題。

你不應該每5秒刷新一次你的應用程序。你應該對意圖結果作出反應。

解決方案1號(ActivityForResult方法)

這是你如何做到這一點:

private static final int SELECT_PHOTO = 100; 

啓動意圖

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
photoPickerIntent.setType("image/*"); 
startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

這部分被稱爲上的結果。你應該用這種方法刷新你的活動。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO: 
     if(resultCode == RESULT_OK){ 
      Uri selectedImage = imageReturnedIntent.getData(); 
      InputStream imageStream = getContentResolver().openInputStream(selectedImage); 
      Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); 
     } 
    } 
} 

編輯: 要清楚:什麼情況是,您創建您的活動。打電話給你的意圖。您的活動正在暫停onPause(),並且在選取之後它將恢復爲onResume()。所以如果你想在暫停後刷新你的活動。您在onResume()方法中編寫此行爲。 但仍然如果你開始一個意圖的一些結果。您可以使用上述方法將結果添加到您的活動中。

解決方案2號(一般的方法)

@Override 
protected void onResume() { 
    super.onResume(); 
    //Refresh here 
}