![]() ![]() ![]() ![]() |
|||||
|
|||||
樓主 youngman ![]()
![]() |
可否麻煩各位指點一下 謝謝!!! |
1樓
最有價值解答
桓祺 ![]() ![]() |
Android 背景執行(Service)
大部分使用者在手機上看到的畫面都是前端的程式(Activity), 如果是要在背景執行的程式,則需要寫成Service並繼承android.app.Service, 由於是在背景執行所以是要寫成Service而不是Activity, 因此需在AndroidManifest新增一個Service。
本篇文章回覆於2011-08-26 13:41
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
2樓 |
[code]
[Activity] start=(Button)findViewById(R.id.start); stop=(Button)findViewById(R.id.stop); start.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(RecordingActivity.this, RecordingService.class); startService(intent); }}); stop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(RecordingActivity.this, RecordingService.class); stopService(intent); }}); [/code] [code] [Service] public class RecordingService extends Service { private MediaRecorder mediaRecorder = null; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onStart(Intent intent,int startId) { String fileName = "record.amr"; try{ File SDCardpath=Environment.getExternalStorageDirectory(); File myDataPath=new File(SDCardpath.getAbsolutePath()+"/download"); if( !myDataPath.exists() ) myDataPath.mkdirs(); File recodeFile = new File(SDCardpath.getAbsolutePath() + "/download/"+fileName); mediaRecorder = new MediaRecorder(); //設定音源 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //設定輸出檔案的格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); //設定編碼格式 mediaRecorder .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //設定錄音檔位置 mediaRecorder.setOutputFile(recodeFile.getAbsolutePath()); mediaRecorder.prepare(); //開始錄音 mediaRecorder.start(); } catch(IOException e){ e.printStackTrace(); } super.onStart(intent, startId); } @Override public void onDestroy() { if(mediaRecorder != null) { mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder = null; } super.onDestroy(); } } [/code] 要記得加入權限喔。
本篇文章回覆於2011-08-26 14:08
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
3樓 | |
4樓
作者回應
youngman ![]() |
感謝~!
本篇文章回覆於2011-08-26 20:35
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
回覆 |
如要回應,請先登入. |