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.