Android利用广播Broadcast接收SMS短信

BroadcastReceiver

[java][/java] view plaincopy

  1. package com.app.test02;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.telephony.SmsMessage;
  7. import android.widget.Toast;
  8. /**
  9.  * 以BroadcastReceiver接收SMS短信
  10.  * */
  11. public class BroadCastTest2_SMS extends BroadcastReceiver{
  12.     public static final String ACTION = “android.provider.Telephony.SMS_RECEIVED”;
  13.     @Override
  14.     public void onReceive(Context context, Intent intent) {
  15.         // TODO Auto-generated method stub
  16.         if (ACTION.equals(intent.getAction())) {
  17.             Intent i = new Intent(context, BroadCastActivity2_SMS.class);
  18.             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  19.             SmsMessage[] msgs = getMessageFromIntent(intent);
  20.             StringBuilder sBuilder = new StringBuilder();
  21.             if (msgs != null && msgs.length > 0 ) {
  22.                 for (SmsMessage msg : msgs) {
  23.                     sBuilder.append(“接收到了短信:\n发件人是:”);
  24.                     sBuilder.append(msg.getDisplayOriginatingAddress());
  25.                     sBuilder.append(“\n——短信内容——-\n”);
  26.                     sBuilder.append(msg.getDisplayMessageBody());
  27.                     i.putExtra(“sms_address”, msg.getDisplayOriginatingAddress());
  28.                     i.putExtra(“sms_body”, msg.getDisplayMessageBody());
  29.                 }
  30.             }
  31.             Toast.makeText(context, sBuilder.toString(), 1000).show();
  32.             context.startActivity(i);
  33.         }
  34.     }
  35.     public static SmsMessage[] getMessageFromIntent(Intent intent) {
  36.         SmsMessage retmeMessage[] = null;
  37.         Bundle bundle = intent.getExtras();
  38.         Object pdus[] = (Object[]) bundle.get(“pdus”);
  39.         retmeMessage = new SmsMessage[pdus.length];
  40.         for (int i = 0; i < pdus.length; i++) {
  41.             byte[] bytedata = (byte[]) pdus[i];
  42.             retmeMessage[i]  = SmsMessage.createFromPdu(bytedata);
  43.         }
  44.         return retmeMessage;
  45.     }
  46. }

AndroidManifest注册

[html][/html] view plaincopy

  1. <receiver android:name=”.BroadCastTest2_SMS”>
  2.     <intent-filter >
  3.         <action android:name=”android.provider.Telephony.SMS_RECEIVED”/>
  4.     </intent-filter>
  5. </receiver>

权限

[html][/html] view plaincopy

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

Activity文件

[java][/java] view plaincopy

  1. package com.app.test02;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. public class BroadCastActivity2_SMS extends Activity{
  7.     private TextView textView;
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         // TODO Auto-generated method stub
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_bc2_sms);
  13.         textView = (TextView) findViewById(R.id.textView1);
  14.         Intent intent = getIntent();
  15.         if (intent != null) {
  16.             String address = intent.getStringExtra(“sms_address”);
  17.             if (address != null) {
  18.                 textView.append(“\n\n发件人:\n” + address);
  19.                 String bodyString = intent.getStringExtra(“sms_body”);
  20.                 if (bodyString != null) {
  21.                     textView.append(“\n短信内容:\n” + bodyString);
  22.                 }
  23.             }
  24.         }
  25.     }
  26. }

Layout布局文件

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:layout_width=”match_parent”
  4.     android:layout_height=”match_parent”
  5.     android:orientation=”vertical”
  6.     android:background=”#fff”
  7.     android:padding=”10dp”>
  8.     <LinearLayout
  9.         android:layout_width=”match_parent”
  10.         android:layout_height=”wrap_content”
  11.         android:orientation=”vertical”
  12.         android:gravity=”center_horizontal” >
  13.         <TextView
  14.             android:id=”@+id/textView1″
  15.             android:layout_width=”wrap_content”
  16.             android:layout_height=”wrap_content”
  17.             android:text=”短信”
  18.             android:textColor=”#000″ />
  19.     </LinearLayout>
  20. </LinearLayout>

效果图

标签