Android Implementation of Basic Short Message Interception Function Code Example
- 2021-07-06 11:45:12
- OfStack
Key points
1. Add permission to "receive" SMS in Manifest. xml
<uses-permission Android:name="android.permission.RECEIVE_SMS"></uses-permission>
2. Register an receive in Manifest. xml
<!-- Registration Receiver, And set the priority -->
<receiver android:name=".AutoSMS" android:exported="false">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
3. Define a SMS receiving class and override onReceive
// Inheritance BroadcastReceiver
public class AutoSMS extends BroadcastReceiver
{
private String TAG="AutSMS";
// Broadcast message type
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
// Covering onReceive Method
@Override
public void onReceive(Context context, Intent intent)
{
.....
Instances
Here's the complete code:
Manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxh.autosms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Registration Receiver, And set the priority -->
<receiver android:name=".AutoSMS" android:exported="false">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
AutoSMS.java:
package com.xxh.autosms;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
// Inheritance BroadcastReceiver
public class AutoSMS extends BroadcastReceiver
{
private String TAG="AutSMS";
// Broadcast message type
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
// Covering onReceive Method
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
Log.i(TAG, " Raise a receive event ");
//StringBuilder body=new StringBuilder("");// Content of SMS
//StringBuilder sender=new StringBuilder("");// Sender
// Judge the broadcast message first
String action = intent.getAction();
if (SMS_RECEIVED_ACTION.equals(action))
{
// Get intent Parameter
Bundle bundle=intent.getExtras();
// Judge bundle Content
if (bundle!=null)
{
// Take pdus Content , Convert to Object[]
Object[] pdus=(Object[])bundle.get("pdus");
// Parse SMS
SmsMessage[] messages = new SmsMessage[pdus.length];
for(int i=0;i<messages.length;i++)
{
byte[] pdu=(byte[])pdus[i];
messages[i]=SmsMessage.createFromPdu(pdu);
}
// Analyze the specific parameters after parsing the content
for(SmsMessage msg:messages)
{
// Get the content of SMS
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);
//TODO: Judge according to conditions , Then enter 1 General treatment
if ("10060".equals(sender))
{
// Shielded mobile phone number is 10060 SMS, it can also be done here 1 Some processing, such as sending this information to the 3 People's cell phones and so on.
//TODO: Test
Toast.makeText(context, " Received 10060 SMS of "+" Content :"+content, Toast.LENGTH_LONG).show();
// For specific content , Cancel a broadcast
abortBroadcast();
}
else
{
Toast.makeText(context, " Received :"+sender+" Content :"+content+" Time :"+sendTime.toString(), Toast.LENGTH_LONG).show();
}
}
}
}//if Judge the end of broadcast message
}
}