2012-01-03 80 views
0

我使用VideoView在我的應用中播放視頻,因此我需要爲每個視頻創建大量活動。我的問題是,無論如何要處理這個問題?因爲到目前爲止我已經創建了100多個活動。由於活動太多,我不希望我的應用變得太大。如何在Android中處理太多Activites?

+0

爲什麼要爲每個視頻創建活動。只需傳遞鏈接並在活動中使用一個視頻視圖並導航視頻「下一步」/「上一步」即可。 – 2012-01-03 05:55:50

+0

你有任何教程嗎? – Leon 2012-01-03 05:57:10

+0

Leon,沒有冒犯,但基於這個問題,不言而喻,你還沒有準備好寫商業級軟件。通過向Android市場發佈另一個半功能,可執行的,bug過多的應用程序,你沒有任何好處。請考慮花時間學習如何在Android和Java中正確構建。鑑於你所描述的應用程序聽起來應該是最多2或3個活動(如果你正確地佈置,1個可能就足夠了)。 – 2012-01-03 06:50:53

回答

0

好,坦白地說,大多數開發商不得不做的功能和低之間的雜耍行爲內存佔用。考慮到各種活動狀態,最好的實施工作已經完成。讀起來吧here

這是最好的,你可以從你身邊做。當其他活動應用需要資源時,Android將始終「殺死」在後臺運行的活動。

+0

所以你說這不能被幫助? – Leon 2012-01-03 04:26:30

+0

根本不是。正確管理活動狀態通常可以完成。 – 2012-01-03 04:28:14

+0

「管理活動狀態」是什麼意思? – Leon 2012-01-03 04:31:39

0

這裏是在單個活動中導航下一個/上一個視頻的代碼。像播放列表。

public class Test11Activity extends Activity { 
private int currentVideo=0; 
ArrayList<String> httpLinkArrayList = new ArrayList<String>(); 
VideoView videoView; 
Button n_button; 
Button p_button; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    httpLinkArrayList.add("http://abb.mp4"); 
    httpLinkArrayList.add("http://abb1.mp4"); 
    httpLinkArrayList.add("http://abb2.mp4"); 
    httpLinkArrayList.add("http://abb3.mp4"); 
    httpLinkArrayList.add("http://abb4.mp4"); 


    LinearLayout mainlayout = new LinearLayout(this); 

mainlayout.setOrientation(LinearLayout.VERTICAL); LinearLayout n_p_layout = new LinearLayout(this);

n_button = new Button(this); 
    n_button.setText("Next"); 
    n_button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub    
      if(currentVideo<httpLinkArrayList.size()){ 
       currentVideo++; 
       videoView.setVideoURI(Uri.parse(httpLinkArrayList.get(currentVideo))); 
       videoView.start(); 
      } 
     } 
    }); 
    p_button = new Button(this); 
    p_button.setText("Previous"); 
    p_button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub    
      if(currentVideo>0){ 
       currentVideo--; 
       videoView.setVideoURI(Uri.parse(httpLinkArrayList.get(currentVideo))); 
       videoView.start(); 
      } 
     } 
    }); 
    n_p_layout.addView(n_button); 
    n_p_layout.addView(p_button);   

    videoView = new VideoView(this); 
    videoView.setVideoURI(Uri.parse(httpLinkArrayList.get(currentVideo))); 
    videoView.start(); 
    videoView.setOnCompletionListener(new OnCompletionListener() { 

     @Override 
     public void onCompletion(MediaPlayer arg0) { 
      // TODO Auto-generated method stub    
      if(currentVideo<httpLinkArrayList.size()){ 
            currentVideo++; 
       videoView.setVideoURI(Uri.parse(httpLinkArrayList.get(currentVideo))); 
       videoView.start(); 
      } 
     } 
    }); 

    mainlayout.addView(n_p_layout); 
    mainlayout.addView(videoView); 

    setContentView(mainlayout); 
} 

}

希望對你有用。:)

+0

你是否介意發佈以及mainlayout? – Leon 2012-01-03 06:29:09

+0

mainlayout是簡單的線性佈局。您可以根據您的要求設置其屬性。 – 2012-01-03 06:32:58

+0

它怎麼沒有控制? – Leon 2012-01-03 06:38:03