Android基础教程之SMS简单发送短信程序
2010-07-15 21:01:00 来源:WEB开发网android:id="@+id/myButton1"
android:layout_width="162px"
android:layout_height="wrap_content"
android:text="发送短信"
android:layout_x="80px"
android:layout_y="302px"
>
< /Button>
< /AbsoluteLayout>
Step 3:主控制程序SMSDemo.java如下:
package com.android.test;
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;
public class SMSDemo extends Activity {
private Button mButton1;
private EditText mEditText1;
private EditText mEditText2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取资源
mEditText1 = (EditText)findViewById(R.id.myEditText1);
mEditText2 = (EditText)findViewById(R.id.myEditText2);
mButton1 = (Button)findViewById(R.id.myButton1);
//发送短信的响应
mButton1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
//获取发送地址和发送内容
String messageAddress = mEditText1.getText().toString();
String messageContent = mEditText2.getText().toString();
//构建一取得default instance的SmsManager对象
SmsManager smsManager = SmsManager.getDefault();
//检查输入内容是否为空,这里为了简单就没有判断是否是号码,短信内容长度的限制也没有做
if(messageAddress.trim().length()!=0 && messageContent.trim().length()!=0)
{
try{
PendingIntent pintent = PendingIntent.getBroadcast(SMSDemo.this, 0, new
更多精彩
赞助商链接