• 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>
  • 相关阅读:
    smarty
    js进阶
    JS 基础
    php之面向对象(2)
    php之面向对象(1)
    PHP之图形处理
    PHP代码分离
    PHP文件上传与安全
    PHP substr截取中文字符出现乱码的问题解疑
    关于学习方法
  • 原文地址:https://www.cnblogs.com/xidongyu/p/5665245.html
Copyright © 2020-2023  润新知