本文实例讲述了Android API开发之SMS短信服务处理和获取联系人的方法。分享给大家供大家参考,具体如下:
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>
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android短信与电话操作技巧汇总》、《Android文件操作技巧汇总》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。