android 发送短信功能

 


首先新建一个android project

在string.xml中定义需要用到的字符串:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="hello">Hello World, MainActivity!>
  4.     <string name="app_name">发短信程序</string>
  5.     <string name="str_input_phone_number">请输入手机号</string>
  6.     <string name="str_input_sms_content">请输入短信内容</string>
  7.     <string name="str_send_sms">发送短信</string>
  8.     <string name="str_remind_input_phone_number">请输入手机号</string>
  9.     <string name="str_remind_sms_send_finish">发送完成</string>
  10. </resources>

在main.xml中编辑界面控件代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <TextView
  8.      android:layout_width="fill_parent"
  9.      android:layout_height="wrap_content"
  10.      android:text="@string/str_input_phone_number"
  11.      />
  12.     <EditText
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="wrap_content"
  15.         android:id="@+id/phone_number_editText"
  16.         />
  17.     <TextView
  18.         android:layout_width="fill_parent"
  19.         android:layout_height="wrap_content"
  20.         android:text="@string/str_input_sms_content"
  21.         />        
  22.     <EditText
  23.         android:layout_width="fill_parent"
  24.         android:layout_height="wrap_content"
  25.         android:id="@+id/sms_content_editText"
  26.         />        
  27.     <Button
  28.         android:layout_width="wrap_content"
  29.         android:layout_height="wrap_content"
  30.         android:text="@string/str_send_sms"
  31.         android:id="@+id/send_sms_button"
  32.         />        
  33. </LinearLayout>

最后在MainActivity中编写发送短信的逻辑代码:
  1. package com.sms.ui;

  2. import java.util.List;

  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.telephony.SmsManager;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;

  11. public class MainActivity extends Activity {
  12.     /** Called when the activity is first created. */
  13.     @Override
  14.     public void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.main);
  17.         
  18.         phone_number_editText = (EditText) findViewById(R.id.phone_number_editText);
  19.         sms_content_editText = (EditText) findViewById(R.id.sms_content_editText);
  20.         send_sms_button = (Button) findViewById(R.id.send_sms_button);
  21.         
  22.         send_sms_button.setOnClickListener(new OnClickListener() {

  23.             @Override
  24.             public void onClick(View arg0) {
  25.                 String phone_number = phone_number_editText.getText().toString().trim();
  26.                 String sms_content = sms_content_editText.getText().toString().trim();
  27.                 if(phone_number.equals("")) {
  28.                     Toast.makeText(MainActivity.this, R.string.str_remind_input_phone_number, Toast.LENGTH_LONG).show();
  29.                 } else {
  30.                     SmsManager smsManager = SmsManager.getDefault();
  31.                     if(sms_content.length() > 70) {
  32.                         List<String> contents = smsManager.divideMessage(sms_content);
  33.                         for(String sms : contents) {
  34.                             smsManager.sendTextMessage(phone_number, null, sms, null, null);
  35.                         }
  36.                     } else {
  37.                      smsManager.sendTextMessage(phone_number, null, sms_content, null, null);
  38.                     }
  39.                     Toast.makeText(MainActivity.this, R.string.str_remind_sms_send_finish, Toast.LENGTH_SHORT).show();
  40.                 }
  41.             }     
  42.         });
  43.     }
  44.     
  45.     private EditText phone_number_editText;
  46.     private EditText sms_content_editText;
  47.     private Button send_sms_button;
  48. }

最后添加权限
 <uses-permission android:name="android.permission.SEND_SMS"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值