互发短信之SmsManager
2011-06-22 09:55
148 查看
短信管理器 : SmsManager
1. 在 Android 2.0 以前 应该使用 android.telephony.gsm.SmsManager
之后应该用 android.telephony.SmsManager;
2. 获取系统默认的短信管理器
SmsManager smsManager = SmsManager.getDefault();
3. 按照每条短信最大字数来拆分短信
List<String> divideContents = smsManager.divideMessage(content);
4. 发送短信
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
-- destinationAddress:目标电话号码
-- scAddress:短信中心号码,测试可以不填
-- text: 短信内容
-- sentIntent:发送 -->中国移动 --> 中国移动发送失败 --> 返回发送成功或失败信号 --> 后续处理 即,这个意图包装了短信发送状态的信息
-- deliveryIntent: 发送 -->中国移动 --> 中国移动发送成功 --> 返回对方是否收到这个信息 --> 后续处理 即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。
5. 声明短信发送权限
* AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>
package com.king.gametall; import java.util.List; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class RecommendActivity extends Activity{ private EditText etData,etFriends; private Button btn; public void onCreate(Bundle onSavedInstance){ super.onCreate(onSavedInstance); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.recommend); etData=(EditText)findViewById(R.id.recommenddata); etFriends=(EditText)findViewById(R.id.friendsdata); btn=(Button)findViewById(R.id.recommendbtn); btn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { String messageAddress=etFriends.getText().toString(); String messageContent=etData.getText().toString(); SmsManager smsManager=SmsManager.getDefault(); List<String> divideContents = smsManager.divideMessage(messageContent); if(messageAddress.trim().length()==11&&messageContent.trim().length()!=0){ PendingIntent pintent=PendingIntent.getBroadcast(RecommendActivity.this, 0, new Intent(), 0); smsManager.sendTextMessage(messageAddress, null, messageContent, pintent, null); Toast.makeText(RecommendActivity.this, "send success", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(RecommendActivity.this, "send error", Toast.LENGTH_SHORT).show(); } } }); } }
启动两个模拟器,一个从Eclipse启动,一个用命令行启动:emulator -avd ***;
然后即可发送短信了。
1555521+端口号即11位手机号。
相关文章推荐
- SmsManager发短信(—)
- SmsManager发短信
- SmsManager发短信的相关内容整理
- SmsManager发短信_随笔
- Android应用开发笔记(1):调用打电话和发短信、收短信接口、发Email (Call, Dial, SMSManager, Broadcast, Email)
- Android练习之SmsManager发短信
- SmsManager发短信-方法与实例
- 调用【直接或间接】打电话和发短信、收短信接口、发Email (Call, Dial, SMSManager, Broadcast, Email)
- SmsManager发短信
- Android应用开发笔记(1):调用打电话和发短信、收短信接口、发Email (Call, Dial, SMSManager, Broadcast, Email)
- 手机发短信常用AT指令
- 终结所有短信猫(GPRS,GSM,CDMA,PHS)的发短信问题,以及想语音告警的问题!
- 用C#实现电脑+数据线+手机模式发短信
- C#实现手机发短信
- symbain发短信
- 免费的午餐——编程利用Google日历API发短信、Email
- SOS紧急求助功能实现(发短信,来电白名单)
- nagios2.X与3.X区别及新版本fetion飞信发短信的方法
- 华为EC169上网卡发短信
- Android换卡自动发短信小程序