zoukankan      html  css  js  c++  java
  • 016_01Service的应用--拨打电话自动录音

     1 package com.example.day16_03phonerecorder;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 
     7 public class MainActivity extends Activity {
     8 
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12         setContentView(R.layout.activity_main);
    13         
    14         Intent intent = new Intent(this, MyPhoneRecordService.class);
    15         startService(intent);    
    16     }     
    17 }
      1 package com.example.day16_03phonerecorder;
      2 
      3 import java.io.IOException;
      4 
      5 import android.app.Service;
      6 import android.content.Intent;
      7 import android.media.MediaRecorder;
      8 import android.media.MediaRecorder.AudioEncoder;
      9 import android.media.MediaRecorder.AudioSource;
     10 import android.media.MediaRecorder.OutputFormat;
     11 import android.os.IBinder;
     12 import android.telephony.PhoneStateListener;
     13 import android.telephony.TelephonyManager;
     14 
     15 public class MyPhoneRecordService extends Service {
     16 
     17     MediaRecorder mMediaRecorder;
     18     boolean recordflag =false;
     19     @Override
     20     public IBinder onBind(Intent intent) {
     21         // TODO Auto-generated method stub
     22         return null;
     23     }
     24 
     25     @Override
     26     public void onCreate() {
     27         // TODO Auto-generated method stub
     28         super.onCreate();
     29         System.out.println("MyPhoneRecordService.onCreate()");
     30     }
     31 
     32     @Override
     33     public int onStartCommand(Intent intent, int flags, int startId) {
     34         // TODO Auto-generated method stub
     35         System.out.println("MyPhoneRecordService.onStartCommand()");
     36 
     37         //服务里要监听电话的状态
     38         //  1,如果发现电话接通--》就是开始录音
     39         //  2.如果发现电话挂断 --》就停止录音        
     40         TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
     41         telephonyManager.listen(new MyphoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);        
     42         return super.onStartCommand(intent, flags, startId);
     43     }
     44 
     45     @Override
     46     public void onDestroy() {
     47         // TODO Auto-generated method stub
     48         super.onDestroy();
     49         System.out.println("MyPhoneRecordService.onDestroy()");
     50     }
     51     
     52     class MyphoneStateListener extends PhoneStateListener{
     53        /*    
     54        *//** Device call state: No activity. *//*
     55         public static final int CALL_STATE_IDLE = 0;
     56         *//** Device call state: Ringing. A new call arrived and is
     57          *  ringing or waiting. In the latter case, another call is
     58          *  already active. *//*
     59         public static final int CALL_STATE_RINGING = 1;
     60         *//** Device call state: Off-hook. At least one call exists
     61           * that is dialing, active, or on hold, and no calls are ringing
     62           * or waiting. *//*
     63         public static final int CALL_STATE_OFFHOOK = 2;*/
     64         
     65         
     66         @Override
     67         public void onCallStateChanged(int state, String incomingNumber) {
     68             // TODO Auto-generated method stub
     69             super.onCallStateChanged(state, incomingNumber);            
     70             switch (state) {
     71             case 0:            
     72                 if (recordflag) {
     73                     //停止录音
     74                     // 8. 停止录音 
     75                     mMediaRecorder.stop(); 
     76                     // 9. 释放资源 
     77                     mMediaRecorder.release();
     78                     recordflag=false;
     79                 }    
     80                 System.out
     81                         .println("MyPhoneRecordService.MyphoneStateListener.onCallStateChanged()电话挂断");
     82                 break;
     83             case 1:
     84                 System.out
     85                         .println("MyPhoneRecordService.MyphoneStateListener.onCallStateChanged()正在响铃");
     86                 
     87                 break;
     88             case 2:
     89                 //开始录音
     90             // 1. 创建一个MediaRecorder对象 
     91                   mMediaRecorder = new MediaRecorder(); 
     92             // 2. 设置音频源
     93                  mMediaRecorder.setAudioSource(AudioSource.MIC); 
     94             // 3. 设置输出格式
     95                  mMediaRecorder.setOutputFormat(OutputFormat.THREE_GPP);                  
     96             // 4. 设置输出文件的名字
     97                  mMediaRecorder.setOutputFile(getFilesDir()+"/record.3pg"); 
     98             // 5. 设置音频编码
     99                  mMediaRecorder.setAudioEncoder(AudioEncoder.DEFAULT); 
    100             // 6. 准备以下, 马上开始录音
    101                  try {
    102                     mMediaRecorder.prepare();
    103                 } catch (IllegalStateException e) {
    104                     // TODO Auto-generated catch block
    105                     e.printStackTrace();
    106                 } catch (IOException e) {
    107                     // TODO Auto-generated catch block
    108                     e.printStackTrace();
    109                 } 
    110             // 7. 开始录音 
    111                  mMediaRecorder.start(); // 录音开始了...         
    112                  recordflag=true;
    113                  
    114                  System.out
    115                         .println("MyPhoneRecordService.MyphoneStateListener.onCallStateChanged()正在通话");
    116                 
    117                 break;
    118 
    119             default:
    120                 break;
    121             }
    122         }    
    123     }
    124 }
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.example.day16_03phonerecorder"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="14"
     9         android:targetSdkVersion="19" />
    10     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    11     <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    12 
    13     <application
    14         android:allowBackup="true"
    15         android:icon="@drawable/ic_launcher"
    16         android:label="@string/app_name"
    17         android:theme="@style/AppTheme" >
    18         <activity
    19             android:name=".MainActivity"
    20             android:label="@string/app_name" >
    21             <intent-filter>
    22                 <action android:name="android.intent.action.MAIN" />
    23 
    24                 <category android:name="android.intent.category.LAUNCHER" />
    25             </intent-filter>
    26         </activity>
    27         <service  android:name=".MyPhoneRecordService"></service>
    28     </application>
    29 
    30 </manifest>

    录音文件保存在/data/data/com.example.day16_03phonerecorder 下

    物随心转,境由心造,一切烦恼皆由心生。
  • 相关阅读:
    Spark5
    Spark4
    Spark3
    冲刺周期二--站立会议01
    团队第一阶段绩效考核
    各个小组对“躲避小球”游戏的评价
    软件项目第一个Sprint评论
    丹佛机场行李处理系统
    冲刺周期一--站立会议07
    冲刺周期一--站立会议06
  • 原文地址:https://www.cnblogs.com/woodrow2015/p/4531857.html
Copyright © 2011-2022 走看看