Android API支持開發可以發送和接收SMS消息的應用程序。目前我們開發過程中使用的Android模擬器還不支持發送SMS,但它可以接收SMS。現在我們來探索一下Android對SMS的支持,我們將會構建一個小小的應用程序來監聽移動設備(或模擬器)上接收到的SMS消息,並將它顯示出來。
我們來定義一個Intent接收器來處理SMS接收事件:
package com.wissen.sms.receiver;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO
}
}
package com.wissen.sms.receiver;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO
}
}
我們需要對這個Intent接收器進行配置以使它能獲取SMS接收事件,‘ android.provider.Telephony.SMS_RECEIVED’這個事件狀態表示了SMS已被接收。我們可以在AndroidManifest.xml中進行如下配置:
<receiver android:name=”.receiver.SMSReceiver” android:enabled=”true”>
<intent-filter>
<action android:name=”android.provider.Telephony.SMS_RECEIVED” />
</intent-filter>
</receiver>
<receiver android:name=”.receiver.SMSReceiver” android:enabled=”true”>
<intent-filter>
<action android:name=”android.provider.Telephony.SMS_RECEIVED” />
</intent-filter>
</receiver>
為了能讓我們的應用能接收SMS,我們得先進行權限的指定,可以在AndroidManifest.xml中如下配置:
<uses-permission android:name=”android.permission.RECEIVE_SMS”></uses-permission>
<uses-permission android:name=”android.permission.RECEIVE_SMS”></uses-permission>
現在,我們的Intent接收器就可以在Android設備接收到SMS的時候被調用了,余下的事情就是去獲取和顯示接收到的SMS消息文本了:
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get(”pdus”);
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
// show first message
Toast toast = Toast.makeText(context, “Received SMS: ” + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get(”pdus”);
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
// show first message
Toast toast = Toast.makeText(context, “Received SMS: ” + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
Android設備接收到的SMS是以pdu形式的(protocol description unit)。android.telephony.gsm.SmsMessage這個類可以儲存SMS的相關信息,我們也可以從接收到的pdu中創建新的SmsMessage實例,Toast界面組件可以以系統通知的形式來顯示接收到的SMS消息文本。
運行程序:
現在讓我們來在模擬器中運行這個應用程序,以及發送SMS消息到這個模擬器上。我們可以在eclipse的Android插件所提供的DDMS視圖(Dalvik Debug Monitor Service)中發送SMS消息到模擬器上(在’Emulator Control’面板中;另外需要指定電話電話號碼,不過可以是任意的)
發出廣播Intent的方法
public static final String MUSIC_ACTION="com.mythlink.MUSIC";
Intent intent=new Intent();
intent.setAction(MUSIC_ACTION);
intent.putExtra("music_path", songPath);
this.sendBroadcast(intent);
public static final String MUSIC_ACTION="com.mythlink.MUSIC";
Intent intent=new Intent();
intent.setAction(MUSIC_ACTION);
intent.putExtra("music_path", songPath);
this.sendBroadcast(intent);
需要再寫一個廣播接收器
public class MusicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();
String music_path=bundle.getString("music_path");
Toast toast=Toast.makeText(context, "Playing music:"+music_path, Toast.LENGTH_LONG);
toast.show();
}
}
public class MusicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();
String music_path=bundle.getString("music_path");
Toast toast=Toast.makeText(context, "Playing music:"+music_path, Toast.LENGTH_LONG);
toast.show();
}
}
獲取聯系人信息
public class ContactsList extends ListActivity {
private ListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c=this.getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
this.startManagingCursor(c);
String[] columns=new String[]{Contacts.People.NAME};
int[] names=new int[]{R.id.song};////////////////
mAdapter = new SimpleCursorAdapter(this, R.layout.song_item, c, columns, names);
this.setListAdapter(mAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i=new Intent(Intent.ACTION_CALL);
Cursor c = (Cursor) mAdapter.getItem(position);
long phoneID = c.getLong(c.getColumnIndex(Contacts.People.PRIMARY_PHONE_ID));
i.setData(ContentUris.withAppendedId(Contacts.Phones.CONTENT_URI, phoneID));
this.startActivity(i);
}
}
public class ContactsList extends ListActivity {
private ListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c=this.getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
this.startManagingCursor(c);
String[] columns=new String[]{Contacts.People.NAME};
int[] names=new int[]{R.id.song};////////////////
mAdapter = new SimpleCursorAdapter(this, R.layout.song_item, c, columns, names);
this.setListAdapter(mAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i=new Intent(Intent.ACTION_CALL);
Cursor c = (Cursor) mAdapter.getItem(position);
long phoneID = c.getLong(c.getColumnIndex(Contacts.People.PRIMARY_PHONE_ID));
i.setData(ContentUris.withAppendedId(Contacts.Phones.CONTENT_URI, phoneID));
this.startActivity(i);
}
}
在Androidmanifest.xml中加入
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<activity android:name=".ContactsList"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>