采用服务窃听电话

来源:互联网 发布:flash网页制作软件 编辑:程序博客网 时间:2024/05/01 11:38

manifest

    <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"/>
        <service android:name=".PhoneStatusService" >        </service>
PhoneStatusService

package org.gentry.callstatuslistener;import android.app.Service;import android.content.Intent;import android.media.MediaRecorder;import android.os.IBinder;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;public class PhoneStatusService extends Service {/** * 长期在后台运行的组件,如果用户不手动的关闭,是不会停止的 */@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {super.onCreate();System.out.println("服务被创建了");// 监视用户电话状态的变化TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); // 电话管理器,电话管理的服务tm.listen(new MyPhoneStatusLinstener(),PhoneStateListener.LISTEN_CALL_STATE); // 监听手机的通话状态的变化}private class MyPhoneStatusLinstener extends PhoneStateListener {private MediaRecorder recorder;@Overridepublic void onCallStateChanged(int state, String incomingNumber) {try {switch (state) {case TelephonyManager.CALL_STATE_IDLE: // 空闲状态,手机没有通话,没有响铃if (recorder != null) {recorder.stop();recorder.reset(); // You can reuse the object by going// back// to// setAudioSource() steprecorder.release(); // Now the object cannot be reusedrecorder = null;}break;case TelephonyManager.CALL_STATE_RINGING: // 响铃状态System.out.println("发现来电号码:" + incomingNumber);// 1. 创建出来一个录音机recorder = new MediaRecorder();recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置录制的音频源,从话筒里面获取声音recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // 文件输出格式recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 声音编码格式recorder.setOutputFile("/sdcard/"+ System.currentTimeMillis() + ".3gp"); // 录音保存的路径recorder.prepare();break;case TelephonyManager.CALL_STATE_OFFHOOK: // 通话状态if (recorder != null) {recorder.start(); // Recording is now started}break;}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}super.onCallStateChanged(state, incomingNumber);}}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("服务被销毁了");}}
MainActivity

package org.gentry.callstatuslistener;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intent = new Intent(this, PhoneStatusService.class);startService(intent);}}





0 0
原创粉丝点击