[Android] Text Message application-real-time acquisition of text message information

Source: Internet
Author: User

We know that you only need to use the code to read text messages in your inbox and text messages in your inbox, but you cannot get them instantly when the text message is sent; if we can get the corresponding information at the instant of the text message, we can expand a lot of applications in turn-that is, remotely operate a mobile phone through the text message.

 

If you want to obtain the information in real time, you need to call the consumer and write a listener class so that we can obtain the short message information in real time.

 

Preview:

 

 

Let's take a look at the code.

 

 

First, we need to create a listener class smsbroadcastreceiver to inherit broadcastreceiver.

 

Initialize a constant action and assign the SMS parameter value.

android.provider.Telephony.SMS_RECEIVED

 

Create the onreceive method.

 

Then, use getaction to monitor the SMS status and use stringbuffer to save the SMS information.

 

And then the main code.

@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(ACTION)) {StringBuffer SMSAddress = new StringBuffer();StringBuffer SMSContent = new StringBuffer();Bundle bundle = intent.getExtras();if (bundle != null) {Object[] pdusObjects = (Object[]) bundle.get("pdus");SmsMessage[] messages = new SmsMessage[pdusObjects.length];for (int i = 0; i < pdusObjects.length; i++) {messages[i] = SmsMessage.createFromPdu((byte[]) pdusObjects[i]);}for (SmsMessage message : messages) {SMSAddress.append(message.getDisplayOriginatingAddress());SMSContent.append(message.getDisplayMessageBody());}}}}

In the code, smsaddress is the number for sending the text message, and smscontent is the text message content.

 

To see whether the two parameters are obtained successfully, print them out.

System. Out. println ("sending number:" + smsaddress + "\ n" + "SMS content:" + smscontent );

But they need to be added to the for loop, because when new information is sent, smsaddress and smscontent will be replaced.

Therefore, if an application is used, it is also determined in the for loop.

 

Finally, register the listener in manifest. xml.

 <receiver android:name="cn.etzmico.SMSBroadcastReceiver"><intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"></action></intent-filter></receiver>

Add permissions.

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

 

In this way, after running the program, smsaddress and smscontent will be assigned a value as long as there is SMS reception.

 

 

Here, I would like to add a knowledge point about the eclipse program.

I believe many beginners do not know that eclipse comes with a text messaging plug-in that can send text messages to virtual machines. In this way, we do not need to start multiple virtual machines at the same time when using text messages ......

What should I do? The method is as follows.

1. Click the window in the menu bar.

2. Find the show view directory.

3. Select other ....

Then, a window will pop up.

4. For ease of operation, enter emulator control in the search bar in the pop-up window.

5. Click emulator control in the list, click OK, or double-click it.

 

In this way, a window appears, with many parameters.

We will have the opportunity to introduce other things in the future. We will use four of them this time.

, We only need to enter the corresponding parameter, select the desired type, and finally click send.

 

PS: Some people wonder why the system is gray and cannot be input or selected because you have not selected the simulator. This plug-in can only send messages to one simulator at a time. The simulator selection is similar to the method used to call the emulator control. The difference is that you can enter devices when entering the emulator control. If you select a simulator, a message is sent to the simulator without entering the simulator number.

 

 

Demo resources: http://download.csdn.net/detail/etzmico/3685332

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.