Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

Android--service服务--MediaRecorder--电话录音

$
0
0


一、通过Intent启动服务:


package com.jiangge.callstatuslistener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Intent intent = new Intent(this, PhoneStatusService.class);
		startService(intent);
	}
}


二、PhoneStatusService.java

package com.jiangge.callstatuslistener;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneStatusService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		System.out.println("===>>>服务被创建了");
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		tm.listen(new MyPhoneStatusListener(), PhoneStateListener.LISTEN_CALL_STATE);// 监听手机的通话状态的变化
	}

	private class MyPhoneStatusListener extends PhoneStateListener {
		MediaRecorder recorder;
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			super.onCallStateChanged(state, incomingNumber);
			try {
				switch (state) {
				case TelephonyManager.CALL_STATE_IDLE: // 空闲状态,没有通话没有响铃
					if (recorder != null) {
						recorder.stop();
						recorder.reset();
						recorder.release();
						recorder = null;
					}
					break;
				case TelephonyManager.CALL_STATE_RINGING: // 响铃状态
					System.out.println("发现来电号码" + incomingNumber);
					recorder = new MediaRecorder();// 创建一个录音机
					recorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置录制的音频源从话筒里面获取声音
					recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
					recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
					recorder.setOutputFile("/sdcard/" + System.currentTimeMillis() + ".3gp");
					recorder.prepare();

					break;
				case TelephonyManager.CALL_STATE_OFFHOOK: // 通话状态
					if (recorder != null) {
						recorder.start();
					}
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("===>>>服务被销毁了");
	}

}

注:/sdcard 不要硬编码

Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath()instead


三、清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jiangge.callstatuslistener"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.jiangge.callstatuslistener.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=".PhoneStatusService" >
        </service>
    </application>

</manifest>

注,权限:

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


效果图:


项目文件结构:



注:MediaRecorder文档中写得很清楚的。



作者:xiaowanggedege 发表于2013-12-3 1:07:03 原文链接
阅读:156 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>