主:
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView micro = (ImageView) findViewById(R.id.imageView1);
micro.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Record.class));
}
});
}
}
记录:
public class Record extends Activity {
MediaPlayer mp = new MediaPlayer();
String name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
record();
}
public void record() {
.
.
.
}
}
答案 0 :(得分:0)
我会说,将mp = new MediaPlayer();
移到onCreate()
也不要忘记添加:
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
}
答案 1 :(得分:0)
试试这种方式
public class Record extends Activity {
MediaPlayer mp;
String name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
mp = new MediaPlayer();
new LongOperation().execute();
}
private class LongOperation extends AsyncTask<Void, Void, Void>
{
@Override
protected void doInBackground(String... params) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// add code for record here.
}
});
}
}
}
答案 2 :(得分:0)
只需将record()
的电话转移到onWindowFocusChanged()
方法
public void onWindowFocusChanged (boolean hasFocus)
{
record();
}
答案 3 :(得分:0)
在单独的(非UI)线程上调用record(),最好使用AsyncTask。 doInBackground将包含记录调用。