设为首页 加入收藏

TOP

Android发送短信
2014-11-24 07:23:57 来源: 作者: 【 】 浏览:1
Tags:Android 发送 短信

AndroidManifest.xml


< xml version="1.0" encoding="UTF-8" >
package="org.me.sendsms">











MainActivity.java



package org.me.sendsms;


import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.List;


publicclass MainActivity extends Activity {


private EditText txtNo;
private EditText txtContent;
private Button btnSend;


/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
txtNo = (EditText) findViewById(R.id.txtNo);
txtContent = (EditText) findViewById(R.id.txtContent);
btnSend = (Button) findViewById(R.id.btnSend);


btnSend.setOnClickListener(new View.OnClickListener() {


@Override
publicvoid onClick(View v) {
String strNo = txtNo.getText().toString();
String strContent = txtContent.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(), 0);
//如果字数超过70,需拆分成多条短信发送 【6688电子商务网站 www.6688.cc 】
if (strContent.length() > 70) {
List msgs = smsManager.divideMessage(strContent);
for (String msg : msgs) {
smsManager.sendTextMessage(strNo, null, msg, sentIntent, null);
}
} else {
smsManager.sendTextMessage(strNo, null, strContent, sentIntent, null);
}
Toast.makeText(MainActivity.this, "短信发送完成", Toast.LENGTH_LONG).show();
}
});
}


}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux C/C++开发工具 Eclipse简单.. 下一篇更换Android控件默认样式

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)
·Linux 常用操作命令 (2025-12-25 16:57:11)
·HTTP协议深度解析: (2025-12-25 16:21:03)
·HTTP 概述 - MDN (2025-12-25 16:21:00)