android服务之录音功能
生活随笔
收集整理的這篇文章主要介紹了
android服务之录音功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
該服務(wù)的作用是當(dāng)打電話時自動錄音。
布局文件
布局文件中開啟錄音服務(wù)
<?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"><Buttonandroid:id="@+id/start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:text="開始錄音"android:onClick="click"/> </LinearLayout>Activity
設(shè)置監(jiān)聽器,啟動一個服務(wù)
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;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}public void click(View v){Toast.makeText(this,"開啟服務(wù)",Toast.LENGTH_SHORT).show();service = new Intent(this, MyService.class);startService(service);}}服務(wù)
在服務(wù)中定義內(nèi)部類來監(jiān)聽電話狀態(tài)
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;@Overridepublic void onCreate() {super.onCreate();//獲取電話管理器tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);//對感興趣的事件進行監(jiān)聽,傳入回調(diào)函數(shù)tm.listen(new MyListener(),PhoneStateListener.LISTEN_CALL_STATE);}class MyListener extends PhoneStateListener{MediaRecorder mRecorder;//一旦電話狀態(tài)改變該方法被調(diào)用 @Overridepublic void onCallStateChanged(int state, String incomingNumber) {super.onCallStateChanged(state, incomingNumber);switch (state){//電話處于空閑狀態(tài)停止錄音case TelephonyManager.CALL_STATE_IDLE:if(mRecorder != null){mRecorder.stop();mRecorder.release();mRecorder = null;}break;//電話處于響鈴狀態(tài)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;//電話處于摘機狀態(tài)case TelephonyManager.CALL_STATE_OFFHOOK:if(mRecorder != null){mRecorder.start();}break;}}}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;} }?
?清單文件
在清單文件中需要獲取相應(yīng)的權(quán)限并注冊服務(wù)
<?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" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid: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>?
轉(zhuǎn)載于:https://www.cnblogs.com/xidongyu/p/5665245.html
總結(jié)
以上是生活随笔為你收集整理的android服务之录音功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sed用法的体会
- 下一篇: win7 绿色版MySQL安装与配置