//==================================Activity.javapackage irdc.EX06_01;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.TextView;public class EX06_01 extends Activity{  private TextView mTextView1;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState){    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    Log.v("debug", "message............");     /*通过findViewById构造器创建TextView对象*/    mTextView1 = (TextView) findViewById(R.id.myTextView1);    mTextView1.setText("等待接收短信...");  }}//==================================Services.javapackage irdc.EX06_01;/*必须引用BroadcastReceiver类*/import android.content.BroadcastReceiver;import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage;/*必须引用Toast类来告知用户收到短信*/import android.widget.Toast; /* 自定义继承自BroadcastReceiver类,聆听系统服务广播的信息 */public class EX06_01_SMSreceiver extends BroadcastReceiver {   /*声明静态字符串,并使用undried.provider.Telephony.SMS_RECEIVED  作为Action为短信的依据*/  private static final String mACTION =   "android.provider.Telephony.SMS_RECEIVED";     @Override   public void onReceive(Context context, Intent intent) {     // TODO Auto-generated method stub     /* 判断传来Intent是否为短信*/    if (intent.getAction().equals(mACTION)){       /*建构一字符串集合变量sb*/      StringBuilder sb = new StringBuilder();       /*接收由Intent传来的数据*/      Bundle bundle = intent.getExtras();       /*判断Intent是有数据*/      if (bundle != null){         /* pdus为 android内置短信参数 identifier         * 通过bundle.get("")返回一包含pdus的对象*/        Object[] myOBJpdus = (Object[]) bundle.get("pdus");         /*构建短信对象array,并依据收到的对象长度来创建array的大小*/        SmsMessage[] messages = new SmsMessage[myOBJpdus.length];          for (int i = 0; i<myOBJpdus.length; i++){            messages[i] =          SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);        }                  /* 将送来的短信合并自定义信息于StringBuilder当中 */          for (SmsMessage currentMessage : messages){            sb.append("接收到来自:\n");            /* 来讯者的电话号码 */           String displayOriginatingAddress2 = currentMessage.getDisplayOriginatingAddress();                   String displayOriginatingAddress = displayOriginatingAddress2;          sb.append(displayOriginatingAddress);          sb.append("\n------传来的短信------\n");            /* 取得传来信息的BODY */            sb.append(currentMessage.getDisplayMessageBody());          }       }          /* 以Notification(Toase)显示来讯信息  */      Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();      //终止广播,manifest.xml的priority优先权很高,终止广播传给其他应用      abortBroadcast();    }   } }//==============================AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="irdc.EX06_01"  android:versionCode="1"  android:versionName="1.0.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">    <activity android:name=".EX06_01"      android:label="@string/app_name">    <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />    </intent-filter>    </activity>    <receiver android:name="EX06_01_SMSreceiver" >     <!-- priority:数值越大,优先权越高,20好像个跟home在同一个优先级上-->   <intent-filter android:priority="20">     <action       android:name="android.provider.Telephony.SMS_RECEIVED" />       </intent-filter>     </receiver>   </application><uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission></manifest> 

更多相关文章

  1. Android(安卓)Intent多种传值方式
  2. android 拖动图片移动效果
  3. android 自带 xml解析
  4. Android隐藏软件盘
  5. android 拖动图片移动效果
  6. 如何判断android 短信发送(sendTextMessage)是否成功
  7. Android(安卓)zip文件压缩解压缩
  8. Android进度条学习一
  9. Android中SQLite增删改查的方法(Android开发视频讲座)

随机推荐

  1. [转]Simple Guide for Porting Android K
  2. 预防Android内存泄露
  3. android 添加一个新的android 键值
  4. android使用字体
  5. @+id/android:list"和"@android:id/list"
  6. Android延时执行方法
  7. android 6 what
  8. Android中实现滑动效果
  9. Android源码仓库和Repo工具使用
  10. 根据终端分配不同的url地址