Android API :SMS簡訊服務處理和擷取連絡人
來源:互聯網
上載者:User
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 {@Overridepublic 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 messageToast 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 {@Overridepublic 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;@Overrideprotected 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);}@Overrideprotected 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>