2013-03-23 58 views
0

我想在onCreate()onResume()中运行相同的功能。功能基本上在10秒内录制,然后停止并播放录制的声音。如何在onCreate()和onResume()方法中调用相同的函数?

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

    new CountDownTimer(
      10000, // 10 second countdown 
      9999) { // onTick time, not used 

     public void onTick(long millisUntilFinished) { 
      // Not used 
     } 

     public void onFinish() { 
      isRecording = false; 
     } 
    }.start(); 

    Thread thread = new Thread(new Runnable() { 

     public void run() { 
      isRecording = true; 
      record(); // starts to record 
     } 
    }); 
    thread.start(); // thread start 
    // thread to start 

    play(); 
} 

如果我打的首页按钮,然后应用程序得到了投入的背景。现在,如果我再次点击该应用程序的图标按钮,我想调用相同的录制和播放功能。

我可以在onResume()这样做同样的事吗?基本上重复相同的事情。

public void onResume() { 
    super.onResume(); 

    new CountDownTimer(
      10000, // 10 second countdown 
      9999) { // onTick time, not used 

     public void onTick(long millisUntilFinished) { 
      // Not used 
     } 

     public void onFinish() { 
      isRecording = false; 
     } 
    }.start(); 

    Thread thread = new Thread(new Runnable() { 

     public void run() { 
      isRecording = true; 
      record(); // starts to record 
     } 
    }); 
    thread.start(); // thread start 

    play(); 
} 
+1

创建您自己的方法? – nhaarman 2013-03-23 20:44:22

+1

不需要在onCreate中完成,只需将代码放入onResume即可。 – 2013-03-23 21:46:07

回答

相关问题