WEB开发网
开发学院手机开发Android 开发 android广播监听短信并显示内容 阅读

android广播监听短信并显示内容

 2010-11-05 00:54:27 来源:WEB开发网   
核心提示:android, 短信, 监听, 广播AndroidManifest.xml中添加Java代码1. < receiver android:name=".receive">2. < intent-filter>3. < action android:name="

android, 短信, 监听, 广播AndroidManifest.xml中添加

Java代码

1. < receiver android:name=".receive">

2. < intent-filter>

3. < action android:name="android.provider.Telephony.SMS_RECEIVED" />

4. < /intent-filter>

5. < /receiver>

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

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

< receiver android:name=".receive">

< intent-filter>

< action android:name="android.provider.Telephony.SMS_RECEIVED" />

< /intent-filter>

< /receiver>

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

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

再写一个广播监听

Java代码

1. public class receive extends BroadcastReceiver

2. {

3. String receiveMsg = "";

4. public void onReceive(Context context, Intent intent)

5. {

6. SmsMessage[] msg= null;

7.

8. if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))

9. {

10. //StringBuilder buf = new StringBuilder();

11. Bundle bundle = intent.getExtras();

12. if (bundle != null) {

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

14. msg= new SmsMessage[pdusObj.length];

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

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

17. }

18.

19.

20. for(int i = 0; i < msg.length; i++)

21. {

22. String msgTxt = msg[i].getMessageBody();

1 2  下一页

Tags:android 广播 监听

编辑录入:coldstar [复制链接] [打 印]
赞助商链接