android四大組件之Service 電話錄音

來源:互聯網
上載者:User

標籤:

首先來點的時候一共有三個狀態, 空閑,響鈴,摘機(接電話)

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

許可權分別是對電話操作,錄音許可權,sd卡許可權

想要開機啟動服務就可以用廣播接受者來做。

package com.example.luyin;import android.app.Service;import android.content.Intent;import android.media.MediaRecorder;import android.os.IBinder;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.widget.Toast;public class MyService extends Service {    private MediaRecorder recorder;    public MyService() {    }    @Override    public IBinder onBind(Intent intent) {        // TODO: Return the communication channel to the service.        throw new UnsupportedOperationException("Not yet implemented");    }    @Override    public void onCreate() {        Toast.makeText(MyService.this, "服務啟動", 1).show();        // TODO Auto-generated method stub        super.onCreate();        // 獲得電話管理器,用來監聽電話的狀態        TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);        // event設定電話只監聽電話狀態改變        manager.listen(new Listen(), PhoneStateListener.LISTEN_CALL_STATE);    }    private class Listen extends PhoneStateListener {        // 電話狀態一旦改變調用        // 通過state的傳回值來確定有沒有來電話        @Override        public void onCallStateChanged(int state, String incomingNumber) {            // TODO Auto-generated method stub            super.onCallStateChanged(state, incomingNumber);            switch (state) {            // 空閑            case TelephonyManager.CALL_STATE_IDLE:                Toast.makeText(MyService.this, "空閑", 1).show();                if(recorder!=null){                    //錄音結束並且釋放資源                    recorder.stop();                    recorder.release();                    recorder=null;                }                break;            // 響鈴            case TelephonyManager.CALL_STATE_RINGING:                Toast.makeText(MyService.this, "響鈴", 1).show();                if (recorder == null) {                    // 錄音機                    recorder = new MediaRecorder();                    // 設定錄音的來源是麥克風                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);                    // 設定錄音的儲存格式                    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);                    // 設定儲存路徑                    recorder.setOutputFile("sdcard/voice.3gp");                    // 設定編碼                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);                    // 準備錄音                    try {                        recorder.prepare();                    } catch (Exception e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }                break;            // 接聽            case TelephonyManager.CALL_STATE_OFFHOOK:                Toast.makeText(MyService.this, "摘機", 1).show();                if(recorder!=null){                    //開始錄音                    recorder.start();                }                break;            }        }    }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.luyin"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>    <uses-permission android:name="android.permission.RECORD_AUDIO"/>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.luyin.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service            android:name="com.example.luyin.MyService" >        </service>    </application></manifest>

android四大組件之Service 電話錄音

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.