[android]통화 녹음

4956 단어 android

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.hw.receiver.demo.receiver"  
    android:versionCode="1"  
    android:versionName="1.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <receiver android:name=".SMSReceive">              
            <intent-filter android:priority="800">  
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />  
            </intent-filter>  
        </receiver>
        <receiver android:name=".PhoneReceiver">              
            <intent-filter android:priority="800">  
                <!-- <action android:name="android.intent.action.NEW_OUTGOING_CALL" />  -->
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>  
        </receiver>    
    </application> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>  
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>  
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>  
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
</manifest>  


import java.io.File;
import java.io.IOException;
import java.util.Set;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;


public class PhoneReceiver extends BroadcastReceiver  {

	private static final String TAG = "PhoneReceiver";
	
	private static MediaRecorder recorder;
	private static boolean callOver = false;
	
	@Override
	public void onReceive(Context context, Intent intent) {

		if(intent.getAction().equals("android.intent.action.PHONE_STATE")){
			String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
			/*				StringBuffer sb = new StringBuffer();
			String incomingNumber = intent.getStringExtra("incoming_number");
			String outgoingNumber = intent.getStringExtra("outgoing_number");
			 
	    	Bundle bundle = intent.getExtras();
			Set<String> set = bundle.keySet();
			for(String key:set){
				sb.append(key).append(":").append(bundle.get(key)).append("
"); } sb.append(intent.getAction()).append("
"); sb.append("IncomingNumber:").append(incomingNumber).append("
"); sb.append("OutGoingNumber:").append(outgoingNumber).append("
"); sb.append("PhoneState:").append(phoneState).append("
"); Toast.makeText(context, sb.toString() , 5000).show(); Log.d(TAG, sb.toString()); */ if(phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)){ if(callOver){ stopRecord(); callOver = false; } Log.d(TAG, "IDLE"); }else if(phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){ callOver = true; startRecord(); Log.d(TAG, "OFFHOOK"); }else if(phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)){ Log.d(TAG, "RINGING"); } } } private void startRecord(){ Log.d(TAG, "startRecord"); String path = "/sdcard/test.mp3"; recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); Log.d(TAG, path); recorder.setOutputFile(path); try { recorder.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } recorder.start(); } private void stopRecord(){ Log.d(TAG, "stopRecord"); Log.d(TAG, ""+recorder); recorder.stop(); recorder.release(); recorder = null; } }

좋은 웹페이지 즐겨찾기