有一条短信pop up提醒(常驻BroadcastReceiver)

在AndroidManifest.xml文件向系统注册一常驻的receiver,并设置这个receiver的intent-filter名称为android.provider.Telephony.SMS_RECEIVED,

这样就可以让该receiver对收到短信事件进行处理。

主activity

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); 
    /*透过findViewById建构巳建立TextView对象*/ 
    mTextView1 = (TextView) findViewById(R.id.myTextView1); 
    mTextView1.setText("等待接收短信..."); 
  }
}


一个继承BroadcastReceiver的类

package irdc.EX06_01;

/*必需引用BroadcastReceiver类*/
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
/*必需引用telephoney.gsm.SmsMessage来收取短信*/
import android.telephony.SmsMessage; 
/*必需引用Toast类来监听用户收到短信*/
import android.widget.Toast; 

/* 自定义继承自BroadcastReceiver类,监听系统服务广播的信息 */
public class EX06_01_SMSreceiver extends BroadcastReceiver 
{ 
   /*声明静态字符串,并使用android.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");  
          /* 来讯者的电话号码 */ 
          sb.append(currentMessage.getDisplayOriginatingAddress());  
          sb.append("\n------传来的短信------\n");  
          /* 取得传来讯息的BODY */  
          sb.append(currentMessage.getDisplayMessageBody());  
        }  
      }    
      /* 北Notification(Toase)显示短信信息  */
      Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show(); 
       
      /* 返并加Activity */ 
      Intent i = new Intent(context, EX06_01.class); 
      /*设定让加Activity以一个新的task来执行*/
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(i); 
    } 
  } 
} 


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聆聽系統廣播訊息 -->
    <receiver android:name="EX06_01_SMSreceiver"> 
    <!-- 設定要捕捉的訊息名稱為provider中Telephony.SMS_RECEIVED -->
  <intent-filter> 
    <action 
      android:name="android.provider.Telephony.SMS_RECEIVED" /> 
  </intent-filter> 
    </receiver>   
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest> 


布局文件
<?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聆聽系統廣播訊息 -->
    <receiver android:name="EX06_01_SMSreceiver"> 
    <!-- 設定要捕捉的訊息名稱為provider中Telephony.SMS_RECEIVED -->
  <intent-filter> 
    <action 
      android:name="android.provider.Telephony.SMS_RECEIVED" /> 
  </intent-filter> 
    </receiver>   
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest> 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值