android服务之录音功能
2016-07-12 22:58
477 查看
该服务的作用是当打电话时自动录音。
布局文件
布局文件中开启录音服务<?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>
相关文章推荐
- 安卓布局中的一点经验
- Android中PopupWindow的使用
- Android Canvas.drawText方法中的坐标参数的正确解释
- Android最好看的圆形进度条ProgressBar
- Android 设置dialog背景全透明无边框
- (4.6.15)千万级用户的 Android 客户端是如何养成的
- 实现多效果ListView
- OnClick点击事件的用法(ListView)
- 加载.so库时UnsatisfiedLinkError异常的一个原因
- Android仿魅族日历首页
- Android资源文件的管理
- Android群英传笔记-3.6.1对现有控件进行拓展(TextView 实现闪动效果)
- "SurfaceView控件"-出现的问题记录
- Android 进程常驻(1)----开篇
- Android手机wifi显示的开关Switch怎么做的
- android中需要牢记的单词(android篇)
- android studio编译项目时出错
- view工作原理-计算视图大小的过程(onMeasure)
- ListView的优化
- Android抽象布局——include、merge 、ViewStub