Android发短信程序

编辑main.xml文件内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<? xml version=”1.0″ encoding=”utf-8″?>
< AbsoluteLayout
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
xmlns:android=“http://schemas.android.com/apk/res/android”
>
< TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“收件人:”
android:textSize=“16sp”
android:layout_x=“0px”
android:layout_y=“12px”
>
</ TextView >
< EditText
android: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 >
< EditText
android: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 >
< Button
android: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 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 Intent(), 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-permission android: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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值