Android - 传入短信听众(ICS)

时间:2012-11-18 10:54:29

标签: android sms broadcastreceiver

我创建了一个BroadcastReceiver,它实际上处理传入的短信。这是在Android 2.3.3中运行良好的代码,但不知怎的,我无法在运行ICS的设备上运行相同的代码。

我的接收者

public class SmsListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("#### SMS Received");
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                        System.out.println("#### SMS Received: " + msg_from);
                        System.out.println("#### Message: " + msgBody);
                    }
                }catch(Exception e){
                           Log.d("Exception caught",e.getMessage());
                }
            }
        }
    }
}

权限

   <receiver android:name=".SmsListener" android:enabled="true"       android:permission="android.permission.RECEIVE_SMS">
      <intent-filter>
         <action android:name="android.provider.Telephony.SMS_RECEIVED" />
      </intent-filter>
   </receiver>    

<uses-permission android:name="android.permission.RECEIVE_SMS" /> 
<uses-permission android:name="android.permission.READ_SMS"/>       

1 个答案:

答案 0 :(得分:0)

实际上我得到了答案,现在最新的安全性增加了android只允许通过可见活动注册广播接收器。