Chinaunix首页 | 论坛 | 博客
  • 博客访问: 388224
  • 博文数量: 61
  • 博客积分: 1546
  • 博客等级: 中尉
  • 技术积分: 708
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-22 20:07
文章分类

全部博文(61)

文章存档

2011年(61)

分类: 嵌入式

2011-06-12 15:34:18

和上一篇文章一样,发短信在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=""
  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. }

在功能清单文件中声明发短信权限:


阅读(39895) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~