Receive SMS and SMS

Source: Internet
Author: User

Receive SMS and SMS

Recently, I read the text message reception content in the first line of code and summarized it.

1. When the mobile phone receives a text message, the system will send a broadcast of android. provider. Telephy. SMS_RECEIVER. This broadcast carries all the data of the text message.

2. first define the xml file

<LinearLayout         android:layout_width="match_parent"        android:layout_height="50dp">                <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:padding="10dp"            android:text="From:"/>                <TextView             android:id="@+id/sender"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"/>        </LinearLayout>                 <LinearLayout         android:layout_width="match_parent"        android:layout_height="50dp">                <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:padding="10dp"            android:text="Content:"/>        <TextView             android:id="@+id/content"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"/>

2. MainActivity

Public class MainActivity extends ActionBarActivity {private TextView sender; private TextView content; private IntentFilter receiverFilter; private MessageReceiver messageReceiver; @ Override protected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); sender = (TextView) findViewById (R. id. sender); content = (TextView) findViewById (R. id. content); receiverFilter = new IntentFilter (); receiverFilter. addAction ("android. provider. telephony. SMS_RECEIVED "); receiverFilter. setPriority (100); messageReceiver = new MessageReceiver (); // The first parameter indicates the method to be executed, and the second parameter indicates the accepted broadcast registerReceiver (messageReceiver, receiverFilter );} @ Override protected void onDestroy () {super. onDestroy (); // dynamically register broadcast. unregisterReceiver (Register);} class messageReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context Context, intent intent) {Bundle bundle = intent. getExtras (); Object [] pdus = (Object []) bundle. get ("pdus"); SmsMessage [] messages = new SmsMessage [pdus. length]; for (int I = 0; I <messages. length; I ++) {messages [I] = SmsMessage. createFromPdu (byte []) pdus [I]);} // obtain the sender's number String address = messages [0]. getOriginatingAddress (); String fullMessage = ""; for (SmsMessage message: messages) {fullMessage + = message. getMessageBody (); // get the text message content} sender. setText (address); content. setText (fullMessage); // abortBroadcast ();}}}

3. Declare the permission in AndroidManifest. xml.
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>

4. Operation Diagram

 

Ps: the acceptance method here seems to be the method before API11, and is not recommended now.

 

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.