140 likes | 314 Views
Android Service -電話錄音. 建國科技大學 資管系 饒瑞佶 2013/7 V1. Android Service -電話錄音. 需要 BroadcastReceiver 廣播是用來通知系統,動作或資料處理已經完成了 例如收到一封簡訊、電池用量有變化或者 有人打電話進來 等等 Service. Android Service -電話錄音. 啟動本 App 或是開機後,服務將自動執行 設定 應用程式 執行中,可以查看執行狀態 錄音檔會在 /sdcard /recorded_calls/. 先修改 AndroidManifest.xml.
E N D
Android Service-電話錄音 建國科技大學 資管系 饒瑞佶 2013/7 V1
Android Service -電話錄音 • 需要 • BroadcastReceiver • 廣播是用來通知系統,動作或資料處理已經完成了 • 例如收到一封簡訊、電池用量有變化或者有人打電話進來等等 • Service
Android Service -電話錄音 • 啟動本App或是開機後,服務將自動執行 • 設定應用程式執行中,可以查看執行狀態 • 錄音檔會在 /sdcard/recorded_calls/
先修改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"/>
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的人之一
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); } }
PhoneListenerService.java publicclass PhoneListenerService extends Service { @Override public IBinder onBind(Intent intent) { // TODO自動產生的方法 Stub returnnull; } }
PhoneListenerService.java @Override publicvoidonCreate() { TelephonyManagertelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(newTeleListine(), PhoneStateListener.LISTEN_CALL_STATE); super.onCreate(); }
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;
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;
PhoneListenerService.java case TelephonyManager.CALL_STATE_RINGING : mobile = incomingNumber; break; default : break; } }catch(Exception e) { e.printStackTrace(); } super.onCallStateChanged(state, incomingNumber); } }
再修改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" />
// 列出所有錄音檔 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); } });