Android发短信程序

时间:2021-07-21 05:25:40

编辑main.xml文件内容为:

123456789101112131415161718192021222324252627282930313233343536373839404142434445 <?xmlversion=”1.0″ encoding=”utf-8″?><AbsoluteLayoutandroid:layout_width=“fill_parent”android:layout_height=“fill_parent”xmlns:android=“http://schemas.android.com/apk/res/android”><TextViewandroid:layout_width=“wrap_content”android:layout_height=“wrap_content”android:text=“收件人:”android:textSize=“16sp”android:layout_x=“0px”android:layout_y=“12px”></TextView><EditTextandroid:id=“@+id/myEditText1″android:layout_width=“fill_parent”android:layout_height=“wrap_content”android:text=“”android:textSize=“18sp”android:layout_x=“60px”android:layout_y=“2px”></EditText><EditTextandroid:id=“@+id/myEditText2″android:layout_width=“fill_parent”android:layout_height=“223px”android:text=“”android:textSize=“18sp”android:layout_x=“0px”android:layout_y=“52px”></EditText><Buttonandroid:id=“@+id/myButton1″android:layout_width=“162px”android:layout_height=“wrap_content”android:text=“发送”android:layout_x=“80px”android:layout_y=“300px”></Button></AbsoluteLayout>

主控制程序SMSDemo.java

package com.sighlife.smsDemo; 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 extendsActivity { privateButton mButton1;privateEditText mEditText1;privateEditText 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(newButton.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,newIntent(), 0);smsManager.sendTextMessage(messageAddress,null,messageContent, pintent,null); } catch(Exception e) {e.printStackTrace();}// 提示发送成功Toast.makeText(SMSDemo.this, “发送成功”, Toast.LENGTH_LONG).show();} else{Toast.makeText(SMSDemo.this, “发送地址或者内容不能为空”,Toast.LENGTH_SHORT).show();}} });}}


增加拨打电话权限

AndroidManifest.xml代码如下:

1 <uses-permissionandroid:name=“android.permission.SEND_SMS”></uses-permission>

使用smsmanager发送的短信不会有系统记录,解决方法

ContentValues values = new ContentValues();
values.put("address", "123456789");
values.put("body", "foo bar");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);