短信电话监听

一、电话监听

package com.shz.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;
import android.util.Log;

public class PhoneStatusService extends Service {

    private final static String TAG = "PhoneStatusService";

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

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "服务创建了...");
        TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        manager.listen(new MyPhoneStateListener(),
                PhoneStateListener.LISTEN_CALL_STATE);
    }

    private class MyPhoneStateListener extends PhoneStateListener {

        /* 权限
         * <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"/>
        */
        
        private MediaRecorder recorder;

        @Override
        public 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() step
                        recorder.release(); // Now the object cannot be reused
                        recorder = null;
                    }
                    break;

                case TelephonyManager.CALL_STATE_RINGING: // 电话响铃
                    Log.i(TAG, "主人来电话了:" + incomingNumber);
                    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();
                    }
                    break;

                default:
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            super.onCallStateChanged(state, incomingNumber);
        }

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "服务销毁了...");
    }

}
View Code

二、短信监听(Android4以上版本必须要运行在带Activity的程序里)

package com.shz.smslistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] pdus = (Object[]) intent.getExtras().get("pdus");
        for(Object pdu : pdus)
        {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
            String body = smsMessage.getMessageBody();
            String sender = smsMessage.getOriginatingAddress();
            
            Log.i("SmsReceiver", "body:"+body+";sender:"+sender);
        }
    }

}
View Code
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shz.smslistener"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <receiver android:name="com.shz.smslistener.SmsReceiver">
            <intent-filter android:priority="1000">
                <action  android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
    </application>

    
</manifest>
View Code

 

posted @ 2014-08-30 22:25  萌浩  阅读(200)  评论(0编辑  收藏  举报