Android上监听收到的SMS

Android SDK1.1 验证可行。

1.实现一个Receiver【收到的是PDU,需要从PDU中还原出SMSMessage】

	public void onReceive(Context ctx, Intent intent) {
		Log.d("SMSReceiver", "a message received");
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			Object[] pdus = (Object[]) bundle.get("pdus");
			SmsMessage[] messages = new SmsMessage[pdus.length];
			for (int i = 0; i < pdus.length; i++) {
				messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
			}

			for (SmsMessage message : messages) {
				String strFrom = message.getDisplayOriginatingAddress();
				String strMsg = message.getDisplayMessageBody();
				Log.d("SMSReceiver","From:"+strFrom);
				Log.d("SMSReceiver","Msg:"+strMsg);
			}
		}
	}

 2.配置应用permission,允许接收SMS。在AndroidManifest.xml中

 

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

 3.配置Receiver的Intent-Filter,可以接收SMS。在AndroidManifest.xml中

        <receiver android:name=".app.SMSReceiver">
        	<intent-filter>
        		<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        	</intent-filter>
        </receiver>

 4.启动模拟器,进入模拟器的console,模拟接收一条短信

    telnet localhost 5554

    sms send 110 aaaaaaaa

    然后在Logcat中就可以看到接收到的sms信息了

 

你可能感兴趣的:(android,xml)