http://www.cnblogs.com/GarfieldTom/archive/2012/08/25/2655609.html
  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.xxh.autosms"  android:versionCode="1"  android:versionName="1.0" >  <uses-sdk    android:minSdkVersion="8"    android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.RECEIVE_SMS"/>  <application    android:icon="@drawable/ic_launcher"    android:label="@string/app_name"    android:theme="@style/AppTheme" >    <activity      android:name=".MainActivity"      android:label="@string/title_activity_main" >      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>    </activity>    <!-- 注册Receiver,并且设置优先级 -->    <receiver android:name=".AutoSMS" android:exported="false">      <intent-filter android:priority="1000">        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>      </intent-filter>    </receiver>     </application></manifest>
  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.xxh.autosms"  android:versionCode="1"  android:versionName="1.0" >  <uses-sdk    android:minSdkVersion="8"    android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.RECEIVE_SMS"/>  <application    android:icon="@drawable/ic_launcher"    android:label="@string/app_name"    android:theme="@style/AppTheme" >    <activity      android:name=".MainActivity"      android:label="@string/title_activity_main" >      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>    </activity>    <!-- 注册Receiver,并且设置优先级 -->    <receiver android:name=".AutoSMS" android:exported="false">      <intent-filter android:priority="1000">        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>      </intent-filter>    </receiver>     </application></manifest>
  
package com.xxh.autosms;import java.text.SimpleDateFormat;import java.util.Date;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.telephony.SmsMessage;import android.util.Log;import android.widget.Toast;//继承BroadcastReceiverpublic class AutoSMS extends BroadcastReceiver{  private String TAG="AutSMS";  //广播消息类型  public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";  //覆盖onReceive方法  @Override  public void onReceive(Context context, Intent intent)  {    // TODO Auto-generated method stub    Log.i(TAG, "引发接收事件");    //StringBuilder body=new StringBuilder("");//短信内容    //StringBuilder sender=new StringBuilder("");//发件人    //先判断广播消息    String action = intent.getAction();    if (SMS_RECEIVED_ACTION.equals(action))    {      //获取intent参数      Bundle bundle=intent.getExtras();      //判断bundle内容      if (bundle!=null)      {        //取pdus内容,转换为Object[]        Object[] pdus=(Object[])bundle.get("pdus");        //解析短信        SmsMessage[] messages = new SmsMessage[pdus.length];        for(int i=0;i<messages.length;i++)        {          byte[] pdu=(byte[])pdus[i];          messages[i]=SmsMessage.createFromPdu(pdu);        }          //解析完内容后分析具体参数        for(SmsMessage msg:messages)        {          //获取短信内容          String content=msg.getMessageBody();          String sender=msg.getOriginatingAddress();          Date date = new Date(msg.getTimestampMillis());          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");          String sendTime = sdf.format(date);          //TODO:根据条件判断,然后进一般处理          if ("10060".equals(sender))          {            // 屏蔽手机号为10060的短信,这里还可以时行一些处理,如把这个信息发送到第三人的手机等等。            //TODO:测试            Toast.makeText(context, "收到10060的短信"+"内容:"+content, Toast.LENGTH_LONG).show();            //对于特定的内容,取消广播            abortBroadcast();          }          else          {            Toast.makeText(context, "收到:"+sender+"内容:"+content+"时间:"+sendTime.toString(), Toast.LENGTH_LONG).show();          }        }             }    }//if 判断广播消息结束  }}

更多相关文章

  1. Android 记录gitignore文件内容
  2. 关于Android 混淆的内容总结
  3. android 读取文件内容操作
  4. Android notification点击跳转到Activity,点击返回跳转,前一条消息
  5. Android消息机制入门一
  6. Android Studio 新建编辑条 点击按钮显示控件中的内容
  7. android intent 最大传送内容 1m

随机推荐

  1. Android studio的gradle教程整理
  2. 检测android的网络链接状态
  3. Android Studio更新3.1后报错
  4. AndroidAZ系列: 四大组件之Activity(All,
  5. Android 解决Dialog导致软键盘无法隐藏的
  6. Android(安卓)RecyclerView adapter noti
  7. Android 标识绑定View
  8. 修改Android应用程序的默认最大内存值
  9. 【Android】getwidth和getmeasuredwidth
  10. Android(安卓)Studio 4.0 New Class 的坑