学习内容: 你将学到如何使用IntentReceiver.响应收到的短消息。一个可能用到的场景是, 播放音乐的时候少到一则 短消息或修改这条短消息.
sdk版本: m5-rc14
问题/疑问: 楼下跟帖。。。
难度: 2 of 5 
界面效果:

描述:
0.)我们想做的是响应Intent "android.provider.Telephony.SMS_RECEIVED", 它是当有收到短消息的时候系统发出的
. 响应这个Intent可以通过一个“消极“的IntentReceiver完成。 这个 IntentReceiver 将显示一个 Notification (像上图那样) 并在之后启动Main-Activity.
注意: AndroidManufest.xml
里必须要求有一个permission 来接收Intent "android.provider.Telephony.SMS_RECEIVED":
XML:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
这是 AndroidManufest.xml的全部代码:
XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.anddev.android.smsexample">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="@drawable/icon">
<!-- The Main Activity that gets started by the IntentReceiver listed below -->
<activity android:name=".SMSActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- This class will react on the SMS show a notification
and start the Main-App afterwards -->
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
1.)现在我们需要实现 IntentReceiver 我们叫做 "SMSReceiver" ,和 MainActivity 我们叫做 "SMSActivity".
Lets workout the "SMSReceiver" first:
The uninteresting parts first:
Java:
package org.anddev.android.smsexample;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
public class SMSReceiver extends IntentReceiver {
/** TAG used for Debug-Logging */
private static final String LOG_TAG = "SMSReceiver";
/** The Action fired by the Android-System when a SMS was received.
* We are using the Default Package-Visibility */
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
这是系统 '调用'IntentReceiver时使用的method. 因为我们把这个类在AndroidManifest.xml t里声明了,所以有短消息来的时候它一定会被调用 
| Java: |
| // @Override public void onReceiveIntent(Context context, Intent intent) { if (intent.getAction().equals(ACTION)) { // if(message starts with SMStretcher recognize BYTE) StringBuilder sb = new StringBuilder(); /* The SMS-Messages are 'hiding' within the extras of the Intent. */ Bundle bundle = intent.getExtras(); if (bundle != null) { /* Get all messages contained in the Intent*/ SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent); /* Feed the StringBuilder with all Messages found. */ for (SmsMessage currentMessage : messages){ sb.append("Received compressed SMS\nFrom: "); /* Sender-Number */ sb.append(currentMessage.getDisplayOriginatingAddress()); sb.append("\n----Message----\n"); /* Actual Message-Content */ sb.append(currentMessage.getDisplayMessageBody()); } } /* Logger Debug-Output */ Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb); /* Show the Notification containing the Message. */ Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show(); /* Consume this intent, that no other application will notice it. */ this.abortBroadcast(); |
最后我们通过下面的代码启动 Main-Activity :
| Java: |
| /* Start the Main-Activity */ Intent i = new Intent(context, SMSActivity.class); i.setLaunchFlags(Intent.NEW_TASK_LAUNCH); context.startActivity(i); } } } |
2.) 这个 SMSActivity 可以支持你脑子里的任何想法, 像一个小的 音乐播放器, 一个 短消息存储程序, 一个... 所有你能想象的可能

就这样了,我希望它能够帮助你.

