1 / 14

Android Service -電話錄音

Android Service -電話錄音. 建國科技大學 資管系 饒瑞佶 2013/7 V1. Android Service -電話錄音. 需要 BroadcastReceiver 廣播是用來通知系統,動作或資料處理已經完成了 例如收到一封簡訊、電池用量有變化或者 有人打電話進來 等等 Service. Android Service -電話錄音. 啟動本 App 或是開機後,服務將自動執行 設定  應用程式  執行中,可以查看執行狀態 錄音檔會在 /sdcard /recorded_calls/. 先修改 AndroidManifest.xml.

violet-best
Download Presentation

Android Service -電話錄音

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android Service-電話錄音 建國科技大學 資管系 饒瑞佶 2013/7 V1

  2. Android Service -電話錄音 • 需要 • BroadcastReceiver • 廣播是用來通知系統,動作或資料處理已經完成了 • 例如收到一封簡訊、電池用量有變化或者有人打電話進來等等 • Service

  3. Android Service -電話錄音 • 啟動本App或是開機後,服務將自動執行 • 設定應用程式執行中,可以查看執行狀態 • 錄音檔會在 /sdcard/recorded_calls/

  4. 先修改AndroidManifest.xml <!-- 電話狀態監聽--> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- 開啟廣播--> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- 錄音權限--> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  5. Main.java publicclass Main extends Activity { @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent receiver = new Intent("rfid.ctu.receiver"); this.sendBroadcast(receiver); } } 註冊在AndroidManifest.xml • 我是可以處理這個BroadcastReceiver的人之一

  6. MyBroadcastReceiver.java publicclass MyBroadcastReceiver extends BroadcastReceiver { @Override publicvoid onReceive(Context arg0, Intent arg1) { // TODO自動產生的方法 Stub Intent service = new Intent(arg0,PhoneListenerService.class); arg0.startService(service); } }

  7. PhoneListenerService.java publicclass PhoneListenerService extends Service { @Override public IBinder onBind(Intent intent) { // TODO自動產生的方法 Stub returnnull; } }

  8. PhoneListenerService.java @Override publicvoidonCreate() { TelephonyManagertelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(newTeleListine(), PhoneStateListener.LISTEN_CALL_STATE); super.onCreate(); }

  9. PhoneListenerService.java privateclass TeleListine extends PhoneStateListener { private String mobile; //來電 private MediaRecorder recorder; //錄音 private File autoFile; //存檔 privatebooleanrecoder; //是否錄音 @Override publicvoid onCallStateChanged(int state,String incomingNumber) { try{ switch(state) { case TelephonyManager.CALL_STATE_IDLE : if(recoder) { recorder.stop(); recorder.release(); recoder = false; } break;

  10. PhoneListenerService.java caseTelephonyManager.CALL_STATE_OFFHOOK : //接聽電話 recorder = newMediaRecorder(); //MIC只錄自己的聲音,如果要錄雙方聲音,需改用MediaRecorder.AudioSource.VOICE_CALL recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); String root_directory = Environment.getExternalStorageDirectory() + "/recorded_calls"; File root_file = new File(root_directory); if(!root_file.exists()) { root_file.mkdir(); } String record_call = root_directory + "/" + mobile + "_" + System.currentTimeMillis() + ".3gp"; File autoFile = new File(record_call); if(!autoFile.exists()) { autoFile.createNewFile(); } //autoFile = new File(getCacheDir(),mobile+ "_" + System.currentTimeMillis() + ".3gp"); recorder.setOutputFile(autoFile.getAbsolutePath()); recorder.prepare(); recorder.start(); recoder = true; break;

  11. PhoneListenerService.java case TelephonyManager.CALL_STATE_RINGING : mobile = incomingNumber; break; default : break; } }catch(Exception e) { e.printStackTrace(); } super.onCallStateChanged(state, incomingNumber); } }

  12. 再修改AndroidManifest.xml <receiver android:name=".MyBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="rfid.ctu.receiver" /> </intent-filter> </receiver> <service android:name=".PhoneListenerService" />

  13. 加入撥放列表

  14. // 列出所有錄音檔 mFileList = (ListView) findViewById(R.id.listView1); String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recorded_calls"; File file=new File(filePath); File[] files = file.listFiles(); for(File mCurrentFile:files){ mFileName.add(mCurrentFile.getName()); } ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,mFileName); mFileList.setAdapter(mAdapter); //點選後撥放 mFileList.setOnItemClickListener(new OnItemClickListener(){ @Override publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { String movieurl = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recorded_calls/" + mFileName.get(arg2).toString(); Intent intent = new Intent(android.content.Intent.ACTION_VIEW); Uri data = Uri.parse(movieurl); intent.setDataAndType(data,"video/3gpp"); startActivity(intent); } });

More Related