android服务之录音功能

该服务的作用是当打电话时自动录音。

布局文件


布局文件中开启录音服务

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="开始录音"
        android:onClick="click"/>
</LinearLayout>

Activity


设置监听器,启动一个服务

package xidian.dy.com.chujia;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Intent service;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    public void click(View v){
        Toast.makeText(this,"开启服务",Toast.LENGTH_SHORT).show();
        service = new Intent(this, MyService.class);
        startService(service);
    }

}

服务


在服务中定义内部类来监听电话状态

package xidian.dy.com.chujia;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.io.IOException;

/**
 * Created by dy on 2016/7/12.
 */
public class MyService extends Service {
    TelephonyManager tm;
    @Override
    public void onCreate() {
        super.onCreate();
        //获取电话管理器
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        //对感兴趣的事件进行监听,传入回调函数
        tm.listen(new MyListener(),PhoneStateListener.LISTEN_CALL_STATE);
    }

    class MyListener extends PhoneStateListener{
        MediaRecorder mRecorder;
        //一旦电话状态改变该方法被调用
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state){
                //电话处于空闲状态停止录音
                case TelephonyManager.CALL_STATE_IDLE:
                    if(mRecorder != null){
                        mRecorder.stop();
                        mRecorder.release();
                        mRecorder = null;
                    }
                    break;
                //电话处于响铃状态
                case TelephonyManager.CALL_STATE_RINGING:
                    mRecorder = new MediaRecorder();
                    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    mRecorder.setOutputFile(Environment.getExternalStorageDirectory().toString() + "/record.3gp");
                    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    try {
                        mRecorder.prepare();
                    } catch (IOException e) {
                        Log.e(this.getClass().getName(), "prepare() failed");
                    }
                    break;
                //电话处于摘机状态
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    if(mRecorder != null){
                        mRecorder.start();
                    }
                    break;
            }
        }
    }

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

 

 清单文件


在清单文件中需要获取相应的权限并注册服务

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xidian.dy.com.chujia">
    <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="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="主界面">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".MyService" />
</application>
</manifest>

 

转载于:https://www.cnblogs.com/xidongyu/p/5665245.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值