Android 中 BroadCast Receiver 可以用來偵聽廣播事件。在使用 Broadcast 之前,必須使用代碼或是在 AndroidManifest.xml 進(jìn)行注冊。
下面的例子實(shí)現(xiàn)使用短信來觸發(fā) AndroidGraphics2DTutorial 中的示例。短信格式為:@demo:xxxx ,xxxx為 示例名稱,比如, 啟動(dòng) Colors 示例, 則向手機(jī)發(fā)送: @demo:Colors . 手機(jī)在收到短信后,先檢測短信格式是否符合 @demo:xxxx, 若符合,這啟動(dòng)對應(yīng)的示例。
在 AndroidGraphics2DTutorial 中添加一個(gè)自定義的 Broadcast Receiver SmsMessageReceiver 用于監(jiān)測接受到的短信:
public class SmsMessageReceiver extends BroadcastReceiver {
private static final String queryString="@demo:";
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras == null)
return;
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
String fromAddress = message.getOriginatingAddress();
String fromDisplayName = fromAddress;
String msg=message.getMessageBody();
if(msg.startsWith(queryString)){
// Trigger the main activity to fire up a dialog
//that shows/reads the received messages
Intent di = new Intent();
di.setClass(context, AndroidGraphics2DTutorial.class);
di.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
di.putExtra(AndroidGraphics2DTutorial.SMS_FROM_ADDRESS_EXTRA,
fromAddress);
di.putExtra(AndroidGraphics2DTutorial.SMS_FROM_DISPLAY_NAME_EXTRA,
fromDisplayName);
di.putExtra(AndroidGraphics2DTutorial.SMS_MESSAGE_EXTRA, msg);
context.startActivity(di);
}
}
}
}
onReceive 會(huì)在 Broadcast 事件發(fā)生是執(zhí)行,這里檢測短信內(nèi)容,如果是以@demo:開頭的,則啟動(dòng)AndroidGraphics2DTutorial Main Activity。
修改 AndroidMainifest.xml
<receiver android:name=".SmsMessageReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
同時(shí)添加 permission ,和 Java ME 類似 Android 某些 API 需要指定對應(yīng)的 Permission 才可以使用。
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
修改 AndroidGraphics2DTutorial.java 來處理 SMS 消息:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getResources();
String[] activity_Names = res.getStringArray(R.array.activity_name);
String[] activity_Infos = res.getStringArray(R.array.activity_info);
for(int i=0;i<activity_Names.length;i++){
ActivityInfo activityInfo=new ActivityInfo();
activityInfo.activityName=activity_Names[i];
activityInfo.activityInfo=activity_Infos[i];
activityInfo.iconIndex=R.drawable.icon1+i;
activityInfos.add(activityInfo);
}
aa=new ActivityInfoAdapter(this,R.layout.activitylist,activityInfos);
setListAdapter(aa);
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
mFromAddress = bundle.getString(SMS_FROM_ADDRESS_EXTRA);
mMessage = bundle.getString(SMS_MESSAGE_EXTRA);
int index=mMessage.indexOf(queryString);
if(index>=0){
String demoName=mMessage.substring(index+queryString.length());
Intent intent = new Intent();
intent.setClassName(this, packgeName+".example." +demoName);
startActivity(intent);
}
}
}
下面來測試,如果使用設(shè)備,則給手機(jī)發(fā)送@demo:Colors . 如果使用模擬器,則可以啟動(dòng)兩個(gè)模擬器:
http://wiki.jikexueyuan.com/project/android-development-tutorial/images/31.png" alt="" />
模擬器左上角數(shù)字5554,5556為模擬器的號(hào)碼。發(fā)送@demo:Paths ,則自動(dòng)觸發(fā) Paths 示例,如果AndroidGraphics2DTutorial 沒有運(yùn)行,手機(jī)收到 SMS 短信后,會(huì)自動(dòng)啟動(dòng)應(yīng)用。