android SDK 1.0 read SMS  

Posted by Mindlern in


SDK 1.0版本已经去掉了Telephony.Sms.Intents.getMessageFromIntent(intent) 这类SMS读取的API

所以,新版的SDK需要自己“解剖”intent来获得短信内容。


下面是逛android google 讨论区时

看到的解决方法


public class ServerMessagesReceiver extends BroadcastReceiver {

 

    static final String ACTION =

"android.provider.Telephony.SMS_RECEIVED";

 

    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(ACTION)) {

                 StringBuilder buf = new StringBuilder();

                 Bundle bundle = intent.getExtras();

                 if (bundle != null) {

                         Object[] pdusObj = (Object[]) bundle.get("pdus");

                         SmsMessage[] messages = new SmsMessage[pdusObj.length];

                         for (int i = 0; i<pdusObj.length; i++) {

                                 messages = SmsMessage.createFromPdu ((byte[]) pdusObj);

                         }

                       }

                     }

                 }



原帖的方法似乎有点问题

就是最后一个循环

我自己用的时候

做了如下的修改

   for (int i = 0; i<pdusObj.length; i++) {

          messages[i] = SmsMessage.createFromPdu ((byte[]) pdusObj[i]);

}


createFromPdu 返回的是SmsMessage对象,不可能赋值给对象数组

另外pdusObj数组也不能cast 为byte[] 数组

故做出上面的修改


PS.怎么代码之后的文字都倾斜了

要手动该HTML代码么

算了 不改了


This entry was posted on 2008年10月4日星期六 at 星期六, 十月 04, 2008 and is filed under . You can follow any responses to this entry through the comments feed .

0 评论

发表评论