免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1640 | 回复: 0

实现 Android 发短信功能 [复制链接]

论坛徽章:
0
发表于 2011-12-21 08:41 |显示全部楼层
和上一篇文章一样,发短信在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. }

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


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP