Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1050218
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-20 19:29:33

 上次成功实现了“拨打电话”后,心里兴奋极了,于是一口气研究了手机发送短信的原理,其实也很简单。

新建SMS,工程如下所示:


     


    ●修改res/values目录下的string.xml文件,如下所示:


  1. xml version="1.0"encoding="utf-8"?>  
  2. <resources>  
  3.    
  4.     <string name="hello">Hello World,SMSActivity!string>  
  5.     <string name="app_name">发送短信string>  
  6.     <string name="mobile">请输入手机号string>  
  7.     <string name="content">请输入短信内容string>  
  8.     <string name="button">发送短信string>  
  9.     <string name="sendSucc">发送成功!string>  
  10. resources>  


     ●修改res/layout目录下的main/xml文件


  1. xml version="1.0"encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.    
  7.     <TextView  
  8.        android:layout_width="fill_parent"  
  9.        android:layout_height="wrap_content"  
  10.        android:text="@string/mobile" />  
  11.    
  12.     <EditText  
  13.        android:id="@+id/mobile"  
  14.        android:layout_width="fill_parent"  
  15.        android:layout_height="wrap_content">  
  16.    
  17.         <requestFocus />  
  18.     EditText>  
  19.    
  20.     <TextView  
  21.        android:id="@+id/textView1"  
  22.        android:layout_width="fill_parent"  
  23.        android:layout_height="wrap_content"  
  24.        android:text="@string/content" />  
  25.    
  26.     <EditText  
  27.        android:id="@+id/content"  
  28.        android:layout_width="fill_parent"  
  29.        android:layout_height="wrap_content"  
  30.        android:layout_weight="0.96"  
  31.        android:inputType="textMultiLine"/>  
  32.    
  33.     <Button  
  34.        android:id="@+id/button"  
  35.        android:layout_width="wrap_content"  
  36.        android:layout_height="wrap_content"  
  37.        android:text="@string/button" />  
  38.    
  39. LinearLayout>  


     这个文件主要用于设计界面,界面预览可以点击eclipse编辑文件区的Graphical  Layout,如下所示:



   

      ●修改src/com/sinosoft目录下的SMSActivity.java文件

 

  1. package com.sinosoft;  
  2.    
  3. import android.app.Activity;  
  4. import android.telephony.SmsManager;  
  5. import android.widget.Toast;  
  6. import android.os.Bundle;  
  7. import android.widget.*;  
  8. import android.view.*;  
  9. import java.util.*;  
  10. public class SMSActivity extends Activity {  
  11.     /** Calledwhen the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         Button buttion=(Button) this.findViewById(R.id.button);  //获取按钮  
  17.         buttion.setOnClickListener(newView.OnClickListener() {   //为按钮设置监听事件  
  18.             
  19.            public void onClick(View v) {  
  20.               // TODO Auto-generatedmethod stub  
  21.               EditText mobileText=(EditText) findViewById(R.id.mobile);   //获得手机号码文本框  
  22.               EditText contentText=(EditText) findViewById(R.id.content); //获得短信文本框  
  23.               String mobile=mobileText.getText().toString();  //获得手机号码  
  24.               String content=contentText.getText().toString();  //获得短信内容  
  25.               SmsManager smsManager=SmsManager.getDefault();  //获取系统短信管理器  
  26.               List list=smsManager.divideMessage(content);  
  27.               for(String l:list){  //如果短信超过70个字,则将短信内容拆分为几条发送  
  28.                   smsManager.sendTextMessage(mobile, null, l, nullnull);  
  29.               }  
  30.            //  Toast.makeText(SMSActivity.this,R.string.success, Toast.LENGTH_LONG).show();  
  31.               Toast.makeText(SMSActivity.this, R.string.sendSucc, Toast.LENGTH_LONG).show();  //添加短信发送成功提醒  
  32.            }  
  33.        });  
  34.     }  
  35. }  


 

    具体解释详见代码注释

 

    ●设置发送短信权限


     在AndroidMainFest/xml加入一句:

就可,代码如下所示:

  

  1. xml version="1.0"encoding="utf-8"?>  
  2. <manifest xmlns:android=""  
  3.     package="com.sinosoft"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk android:minSdkVersion="10"/>  
  8.     <uses-permission  android:name="android.permission.SEND_SMS"/>  
  9.     <application  
  10.        android:icon="@drawable/ic_launcher"  
  11.        android:label="@string/app_name" >  
  12.         <activity  
  13.            android:name=".SMSActivity"  
  14.            android:label="@string/app_name" >  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.                 <category android:name="android.intent.category.LAUNCHER" />  
  18.             intent-filter>  
  19.         activity>  
  20.     application>  
  21.    
  22. manifest>  

     ●运行程序


     运行此程序后发送短信的界面如下所示:




     输入“手机号码”与“短信内容”,如果发送成功,则会提示你“发送成功”:




这是接收到的手机短信:




       很简单吧,其实实现了这2个小心愿之后我决定要回过头好好研究这个项目各个文件的作用,这是下节的开始的内容,小例子就学到这儿...


       这是本人学习的结果,欢迎转载,欢迎交流,但转载务必给出本文章的链接地址:http://blog.csdn.net/youqishini/article/details/7371347,谢谢~


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