欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android应用开发:电话监听和录音代码示例

程序员文章站 2022-06-04 12:18:36
在oncreate 中执行:复制代码 代码如下:public void oncreate() {  super.oncreate();  log.i("...

在oncreate 中执行:

复制代码 代码如下:

public void oncreate() {
  super.oncreate();
  log.i("tag", "服务启动了");

  // 对电话的来电状态进行监听
  telephonymanager telmanager = (telephonymanager) this
    .getsystemservice(context.telephony_service);
  // 注册一个监听器对电话状态进行监听
  telmanager.listen(new myphonestatelistener(),
    phonestatelistener.listen_call_state);
}


实现myphonestatelistener:
复制代码 代码如下:

private class myphonestatelistener extends phonestatelistener {
  mediarecorder recorder;
  file audiofile;
  string phonenumber;

  public void oncallstatechanged(int state, string incomingnumber) {
   switch (state) {
   case telephonymanager.call_state_idle: /* 无任何状态时 */
    if (recorder != null) {
     recorder.stop(); //停止刻录
     recorder.reset(); //重设
     recorder.release(); //刻录完成一定要释放资源
    }
    break;
   case telephonymanager.call_state_offhook: /* 接起电话时 */

    try {
     recorder = new mediarecorder();
     recorder.setaudiosource(mediarecorder.audiosource.mic); // 设置音频采集原
     recorder.setoutputformat(mediarecorder.outputformat.three_gpp); //内容输出格式
     recorder.setaudioencoder(mediarecorder.audioencoder.amr_nb); //音频编码方式

     // recorder.setoutputfile("/sdcard/myvoice.amr");
     audiofile = new file(
       environment.getexternalstoragedirectory(),
       phonenumber + "_" + system.currenttimemillis()
         + ".3gp");
     recorder.setoutputfile(audiofile.getabsolutepath());
     log.i("tag", audiofile.getabsolutepath());

     recorder.prepare(); //预期准备
     recorder.start();

    } catch (illegalstateexception e) {
     e.printstacktrace();
    } catch (ioexception e) {
     e.printstacktrace();
    }

    break;
   case telephonymanager.call_state_ringing: /* 电话进来时 */
    phonenumber = incomingnumber;
    break;
   default:
    break;
   }
   super.oncallstatechanged(state, incomingnumber);
  }
 }


通过上续两个对应的步骤即可以实现通过server对电话的监听工作,在call_state_idle无状态(即空闲状态)、call_state_offhook 接通电话(即挂起)、call_state_ringing 电话进来(即来电时)这几个状态中执行。

附:android拍照、录像、录音代码范例

复制代码 代码如下:

package com.cons.dcg.collect;

import java.io.file;
import java.text.simpledateformat;
import java.util.*;
import android.app.*;
import android.content.intent;
import android.database.cursor;
import android.net.uri;
import android.os.asynctask;
import android.os.bundle;
import android.os.environment;
import android.provider.mediastore;
import android.view.*;
import android.widget.*;

public class recordactivity extends activity implements onclicklistener {

        private static final int result_capture_image = 1;// 照相的requestcode
        private static final int request_code_take_video = 2;// 摄像的照相的requestcode
        private static final int result_capture_recorder_sound = 3;// 录音的requestcode

        private string strimgpath = "";// 照片文件绝对路径
        private string strvideopath = "";// 视频文件的绝对路径
        private string strrecorderpath = "";// 录音文件的绝对路径

        @override
        protected void oncreate(bundle savedinstancestate) {
                super.oncreate(savedinstancestate);
                this.setcontentview(r.layout.problem_report);
        }

        @override
        protected void onactivityresult(int requestcode, int resultcode, intent data) {
                super.onactivityresult(requestcode, resultcode, data);
                switch (requestcode) {
                case result_capture_image://拍照
                        if (resultcode == result_ok) {
                                toast.maketext(this, strimgpath, toast.length_short).show();
                        }
                        break;
                case request_code_take_video://拍摄视频
                        if (resultcode == result_ok) {
                                uri urivideo = data.getdata();
                                cursor cursor=this.getcontentresolver().query(urivideo, null, null, null, null);
                                if (cursor.movetonext()) {
                                        /** _data:文件的绝对路径 ,_display_name:文件名 */
                                        strvideopath = cursor.getstring(cursor.getcolumnindex("_data"));
                                        toast.maketext(this, strvideopath, toast.length_short).show();
                                }
                        }
                        break;
                case result_capture_recorder_sound://录音
                        if (resultcode == result_ok) {
                                uri urirecorder = data.getdata();
                                cursor cursor=this.getcontentresolver().query(urirecorder, null, null, null, null);
                                if (cursor.movetonext()) {
                                        /** _data:文件的绝对路径 ,_display_name:文件名 */
                                        strrecorderpath = cursor.getstring(cursor.getcolumnindex("_data"));
                                        toast.maketext(this, strrecorderpath, toast.length_short).show();
                                }
                        }
                        break;
                }
        }

      

        /**
         * 照相功能
         */
        private void cameramethod() {
                intent imagecaptureintent = new intent(mediastore.action_image_capture);
                strimgpath = environment.getexternalstoragedirectory().tostring() + "/consdcgmpic/";//存放照片的文件夹
                string filename = new simpledateformat("yyyymmddhhmmss").format(new date()) + ".jpg";//照片命名
                file out = new file(strimgpath);
                if (!out.exists()) {
                        out.mkdirs();
                }
                out = new file(strimgpath, filename);
                strimgpath = strimgpath + filename;//该照片的绝对路径
                uri uri = uri.fromfile(out);
                imagecaptureintent.putextra(mediastore.extra_output, uri);
                imagecaptureintent.putextra(mediastore.extra_video_quality, 1);
                startactivityforresult(imagecaptureintent, result_capture_image);

        }

        /**
         * 拍摄视频
         */
        private void videomethod() {
                intent intent = new intent(mediastore.action_video_capture);
                intent.putextra(mediastore.extra_video_quality, 0);
                startactivityforresult(intent, request_code_take_video);
        }

        /**
         * 录音功能
         */
        private void soundrecordermethod() {
                intent intent = new intent(intent.action_get_content);
                intent.settype("audio/amr");
                startactivityforresult(intent, result_capture_recorder_sound);
        }

        /**
         * 提示信息
         * @param text
         * @param duration
         */
        private void showtoast(string text, int duration) {
                toast.maketext(problemreport.this, text, duration).show();
        }
}