• 采用服务窃听电话示例


    1、创建MainActivity.java类

    package com.example.callstatuslintener;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //开启服务
            Intent intent=new Intent(this, PhoneStatusService.class);
            startService(intent);
        }
    
    }
    View Code

    2、创建PhoneStatusService服务

    /**
     * 
     */
    package com.example.callstatuslintener;
    
    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaRecorder;
    import android.os.IBinder;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    
    /**
     * 长期后台运行的组件,如果用户不手动关闭,不会停止的
     *
     * 2013-12-6
     */
    public class PhoneStatusService extends Service {
    
        /* (non-Javadoc)
         * @see android.app.Service#onBind(android.content.Intent)
         */
        
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            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;
            
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                
                try {
                    switch (state) {
                    case TelephonyManager.CALL_STATE_IDLE: //空闲状态,没有通话没有响铃
                        if (recorder != null) {
                            recorder.stop();
                            recorder.reset();
                            recorder.release();
                        }
                        break;
                    case TelephonyManager.CALL_STATE_RINGING: //响铃状态
                        System.out.println("发现来电号码:" + incomingNumber);
                        //1.创建出来一个录音机
                        recorder = new MediaRecorder();
                        //2.设置录制的音频源从话筒里面获取声音
                        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        //文件格式
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        //2.设置文件保存路劲
                        recorder.setOutputFile("/sdcard/"+ System.currentTimeMillis() + ".3gp");
                        recorder.prepare();
    
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK: //通话状态
                        if (recorder != null) {
                            recorder.start();
                        }
    
                        break;
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
            
        }
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            System.out.println("服务被销毁了……");
        }
        
    }
    View Code

    3、声明AndroidManifest.xml权限

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.callstatuslintener"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
        <uses-permission android:name="android.permission.RECORD_AUDIO"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.callstatuslintener.MainActivity"
                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="com.example.callstatuslintener.PhoneStatusService"></service>
        </application>
    
    </manifest>
    View Code

     demo下载

  • 相关阅读:
    我的大学(转作者袋袋鼠)
    Reporting Services使用问题
    爱的真谛(转)
    无题
    工作???
    带老弟做项目,凉了
    学妹问,学网站开发还是打 ACM?
    在字节跳动的实习经历分享 | 万字求职指南
    云开发是啥?看看它在编程导航项目的实践
    我删库跑路失败了
  • 原文地址:https://www.cnblogs.com/hyzhou/p/3464776.html
Copyright © 2020-2023  润新知