android 通話錄音實現

來源:互聯網
上載者:User

因受系統限制,只能錄自已麥的聲音,錄不到對方的聲音,可能需要改核心才能實現雙向錄音;

接通電話和掛斷電話時,震動一下;

使用廣播接收者實現自啟動; 

 

服務代碼:

 

package com.eboy.phoneListener;

import java.io.File;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.os.Vibrator;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class phoneService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
    }
    
    private final class PhoneListener extends PhoneStateListener{
        private MediaRecorder mediaRecorder;
        private File file;
        private Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            try {
                
                switch (state){
                case TelephonyManager.CALL_STATE_RINGING: //來電
                    break;
                    
                case TelephonyManager.CALL_STATE_OFFHOOK: //接通電話
                    file = new File(Environment.getExternalStorageDirectory(), incomingNumber + System.currentTimeMillis() + ".3gp");
                    mediaRecorder = new MediaRecorder();
                    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    mediaRecorder.setOutputFile(file.getAbsolutePath());
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                    Toast.makeText(getApplicationContext(), "電話已接通,開始錄音。", Toast.LENGTH_SHORT).show();        
                    //震動一下
                    vibrator.vibrate(100);
                    break;
                    
                case TelephonyManager.CALL_STATE_IDLE: //掛斷電話
                    if (mediaRecorder != null){
                        mediaRecorder.stop();
                        mediaRecorder.release();
                        mediaRecorder = null;
                        Toast.makeText(getApplicationContext(), "電話已掛斷,錄音停止。", Toast.LENGTH_SHORT).show();
                        //震動一下
                        vibrator.vibrate(100);
                    }
                    break;
                }
            
            }catch (Exception e) {
                e.printStackTrace();
            }
        }        
    }
}

 

 

通過廣播接收者實現自啟動,代碼:

package com.eboy.phoneListener;

import android.content.Context;
import android.content.Intent;

public class BroadcastReceiver extends android.content.BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Intent service = new Intent(arg0, phoneService.class);
        arg0.startService(service);
    }
}

 

main.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eboy.phoneListener"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />

    <application android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme">
        
        <service android:name=".phoneService"></service>
        <receiver android:name=".BroadcastReceiver">
            <intent-filter >
                
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

    </application>
    
    <!-- 讀取電話狀態許可權 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <!-- SD卡讀寫權限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <!-- 錄音許可權 -->
    <uses-permission android:name="android.permission.RECORD_AUDIO"/> 
    <!-- 震動許可權 -->
    <uses-permission android:name="android.permission.VIBRATE"/>

</manifest>

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.