概述
参考链接: http://www.cnblogs.com/xirihanlin/archive/2009/10/18/1585624.html
归纳关键点:
归纳关键点:
1. manifest.xml中必须指明访问SMS的权限
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
2.使用SmsManager类发送短信(观察内部过程,可以注册BroadcastReciver进行观察)
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent. getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent. getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//注册BroadcastReceiver
registerReceiver( new BroadcastReceiver(){ }, new IntentFilter(SENT));
registerReceiver( new BroadcastReceiver(){ }, new IntentFilter());DELIVERED
SmsManager sms = SmsManager. getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
3. 注册BroadcastReceiver对象接收消息广播,使用Toast类浮动显示
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get( "pdus");
msgs = new SmsMessage[pdus.length ];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage. createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "n";
}
//---display the new SMS message---
Toast. makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
最后
以上就是拼搏黄蜂为你收集整理的Android SMS发送和接收(示例)的全部内容,希望文章能够帮你解决Android SMS发送和接收(示例)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复