Android API开发之SMS短信服务处理和获取联系人的方法
本文实例讲述了AndroidAPI开发之SMS短信服务处理和获取联系人的方法。分享给大家供大家参考,具体如下:
AndroidAPI支持开发可以发送和接收SMS消息的应用程序。目前我们开发过程中使用的Android模拟器还不支持发送SMS,但它可以接收SMS。现在我们来探索一下Android对SMS的支持,我们将会构建一个小小的应用程序来监听移动设备(或模拟器)上接收到的SMS消息,并将它显示出来。
我们来定义一个Intent接收器来处理SMS接收事件:
packagecom.wissen.sms.receiver;
publicclassSMSReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
//TODO
}
}
packagecom.wissen.sms.receiver;
publicclassSMSReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
//TODO
}
}
我们需要对这个Intent接收器进行配置以使它能获取SMS接收事件,android.provider.Telephony.SMS_RECEIVED这个事件状态表示了SMS已被接收。我们可以在AndroidManifest.xml中进行如下配置:
<receiverandroid:name=".receiver.SMSReceiver"android:enabled="true"> <intent-filter> <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> <receiverandroid:name=".receiver.SMSReceiver"android:enabled="true"> <intent-filter> <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
为了能让我们的应用能接收SMS,我们得先进行权限的指定,可以在AndroidManifest.xml中如下配置:
<uses-permissionandroid:name="android.permission.RECEIVE_SMS"></uses-permission> <uses-permissionandroid:name="android.permission.RECEIVE_SMS"></uses-permission>
现在,我们的Intent接收器就可以在Android设备接收到SMS的时候被调用了,余下的事情就是去获取和显示接收到的SMS消息文本了:
publicvoidonReceive(Contextcontext,Intentintent){
Bundlebundle=intent.getExtras();
Objectmessages[]=(Object[])bundle.get("pdus");
SmsMessagesmsMessage[]=newSmsMessage[messages.length];
for(intn=0;n<messages.length;n++){
smsMessage[n]=SmsMessage.createFromPdu((byte[])messages[n]);
}
//showfirstmessage
Toasttoast=Toast.makeText(context,"ReceivedSMS:"+smsMessage[0].getMessageBody(),Toast.LENGTH_LONG);
toast.show();
}
publicvoidonReceive(Contextcontext,Intentintent){
Bundlebundle=intent.getExtras();
Objectmessages[]=(Object[])bundle.get("pdus");
SmsMessagesmsMessage[]=newSmsMessage[messages.length];
for(intn=0;n<messages.length;n++){
smsMessage[n]=SmsMessage.createFromPdu((byte[])messages[n]);
}
//showfirstmessage
Toasttoast=Toast.makeText(context,"ReceivedSMS:"+smsMessage[0].getMessageBody(),Toast.LENGTH_LONG);
toast.show();
}
Android设备接收到的SMS是以pdu形式的(protocoldescriptionunit)。android.telephony.gsm.SmsMessage这个类可以储存SMS的相关信息,我们也可以从接收到的pdu中创建新的SmsMessage实例,Toast界面组件可以以系统通知的形式来显示接收到的SMS消息文本。
运行程序:
现在让我们来在模拟器中运行这个应用程序,以及发送SMS消息到这个模拟器上。我们可以在eclipse的Android插件所提供的DDMS视图(DalvikDebugMonitorService)中发送SMS消息到模拟器上(在'EmulatorControl'面板中;另外需要指定电话电话号码,不过可以是任意的)
发出广播Intent的方法:
publicstaticfinalStringMUSIC_ACTION="com.mythlink.MUSIC";
Intentintent=newIntent();
intent.setAction(MUSIC_ACTION);
intent.putExtra("music_path",songPath);
this.sendBroadcast(intent);
publicstaticfinalStringMUSIC_ACTION="com.mythlink.MUSIC";
Intentintent=newIntent();
intent.setAction(MUSIC_ACTION);
intent.putExtra("music_path",songPath);
this.sendBroadcast(intent);
需要再写一个广播接收器:
publicclassMusicReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
Bundlebundle=intent.getExtras();
Stringmusic_path=bundle.getString("music_path");
Toasttoast=Toast.makeText(context,"Playingmusic:"+music_path,Toast.LENGTH_LONG);
toast.show();
}
}
publicclassMusicReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
Bundlebundle=intent.getExtras();
Stringmusic_path=bundle.getString("music_path");
Toasttoast=Toast.makeText(context,"Playingmusic:"+music_path,Toast.LENGTH_LONG);
toast.show();
}
}
获取联系人信息:
publicclassContactsListextendsListActivity{
privateListAdaptermAdapter;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
Cursorc=this.getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null);
this.startManagingCursor(c);
String[]columns=newString[]{Contacts.People.NAME};
int[]names=newint[]{R.id.song};////////////////
mAdapter=newSimpleCursorAdapter(this,R.layout.song_item,c,columns,names);
this.setListAdapter(mAdapter);
}
@Override
protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
super.onListItemClick(l,v,position,id);
Intenti=newIntent(Intent.ACTION_CALL);
Cursorc=(Cursor)mAdapter.getItem(position);
longphoneID=c.getLong(c.getColumnIndex(Contacts.People.PRIMARY_PHONE_ID));
i.setData(ContentUris.withAppendedId(Contacts.Phones.CONTENT_URI,phoneID));
this.startActivity(i);
}
}
publicclassContactsListextendsListActivity{
privateListAdaptermAdapter;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
Cursorc=this.getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null);
this.startManagingCursor(c);
String[]columns=newString[]{Contacts.People.NAME};
int[]names=newint[]{R.id.song};////////////////
mAdapter=newSimpleCursorAdapter(this,R.layout.song_item,c,columns,names);
this.setListAdapter(mAdapter);
}
@Override
protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
super.onListItemClick(l,v,position,id);
Intenti=newIntent(Intent.ACTION_CALL);
Cursorc=(Cursor)mAdapter.getItem(position);
longphoneID=c.getLong(c.getColumnIndex(Contacts.People.PRIMARY_PHONE_ID));
i.setData(ContentUris.withAppendedId(Contacts.Phones.CONTENT_URI,phoneID));
this.startActivity(i);
}
}
在Androidmanifest.xml中加入:
<uses-permissionandroid:name="android.permission.READ_CONTACTS"/> <activityandroid:name=".ContactsList" android:label="@string/app_name"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid: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程序设计有所帮助。