安卓之实现一个简单的短信发送功能

转自:http://blog.csdn.net/u012561176/article/details/22993063

1.首先,新建一个安卓项目,打开项目下的res下的layout,里面有个activity_main.xml布局文件,打开来,修改里面的代码,其中第一个EditText下的android:inputType="phnoe"这条代码是指定编辑框的类型为电话号码,即0-9的阿拉伯数字,其中,第二个EditText下的android:minLines="3"这条代码是设置此编辑框最小的行数为3行,在界面显示下将会出现3行的编辑框,接下来,我将附上布局文件的代码:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity" >  
  11.       
  12.     <!-- 定义一个文本视图控件 -->  
  13.     <TextView  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/number" />  
  17.       
  18.     <!-- 定义一个文本编辑框控件,即可输入电话号码 -->  
  19.     <EditText   
  20.         android:id="@+id/number"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:inputType="phone"/>  
  24.       
  25.     <!-- 定义一个文本视图控件 -->  
  26.     <TextView  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="@string/content" />  
  30.       
  31.     <!-- 定义一个文本编辑框控件,即可输入短信内容-->  
  32.     <EditText   
  33.         android:id="@+id/content"  
  34.         android:layout_width="match_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:minLines="3"/>  
  37.       
  38.     <!-- 定义一个按钮 -->  
  39.     <Button   
  40.         android:id="@+id/button1"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"  
  43.         android:text="@string/button"/>  
  44. </LinearLayout>  

 

2.接下来,打开安卓项目中src下的MainActivity.java文件,在里面添加些代码,下面,将附上MainActivity.java的代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.sms;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.telephony.SmsManager;  
  8. import android.view.Menu;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;  
  13.   
  14. public class MainActivity extends Activity {  
  15.     private EditText numberText;  
  16.     private EditText contentText;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         numberText=(EditText)findViewById(R.id.number);//获取第一个EditText,即输入电话号码的组件  
  22.         contentText=(EditText)findViewById(R.id.content);//获取第二个EditText,即输入短信内容的组件  
  23.         Button button1=(Button)findViewById(R.id.button1);//获取按钮组件,即发送按钮组件  
  24.         button1.setOnClickListener(new ButtonClickListener());//为发送短信添加一个单击监听器  
  25.     }  
  26.     private final class ButtonClickListener implements View.OnClickListener{  
  27.         @Override  
  28.         public void onClick(View v){  
  29.             String number=numberText.getText().toString();//获取第一个文本编辑框里的输入内容,即输入什么电话号码  
  30.             String content=contentText.getText().toString();//获取第二个文本编辑框里的输入内容,即要发送的短信内容  
  31.             SmsManager manager=SmsManager.getDefault();//获得发送短信的管理器,使用的是android.telephony.SmsManager  
  32.             ArrayList<String> texts=manager.divideMessage(content);  
  33.             for(String text:texts){  
  34.                 //使用短信管理器发送短信内容  
  35.                 //参数一为短信接收者  
  36.                 //参数三为短信内容  
  37.                 //其他可以设为null  
  38.             manager.sendTextMessage(number, null, text, nullnull);  
  39.             }  
  40.             Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_LONG).show();//Toast,用来显示发送成功的提示  
  41.         }  
  42.     }  
  43.     @Override  
  44.     public boolean onCreateOptionsMenu(Menu menu) {  
  45.         // Inflate the menu; this adds items to the action bar if it is present.  
  46.         getMenuInflater().inflate(R.menu.main, menu);  
  47.         return true;  
  48.     }  
  49.   
  50. }  

 

3.以上是完整的代码,但是还要对其设置可以发送短信的权限。


4.接下来设置一下权限,在项目下的AndroidManifest要添加多一条代码,即在安卓配置文件下设置可以发送短信的权限,那条代码为: <uses-permission android:name="android.permission.SEND_SMS"/>,这个权限经常会忘记设置,大家要记得写上哦,不然,运行将会无法实现发送短信的功能,按下发送按钮将会报错,接下来附上安卓配置文件的代码,即AndroidManifest.xml的代码:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.sms"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="18" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/start_b"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.sms.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.       
  27.     <!-- 设置可以发送短信的权限的代码 -->  
  28. <uses-permission android:name="android.permission.SEND_SMS"/>  
  29. </manifest>  


5.其中的电话号码和短信内容是我自己在模拟器里打下去的,其模拟器运行显示如下:

 

6.要实验一下是否能实现发送短信的功能,如果你是部署到真机调试的话,可直接输入电话号码和短信内容,便可发送短信,如果是用模拟器的话,可开启2个模拟器,模拟器中的那个5554:AVD中的5554便是它本身的电话号码,启动第二个模拟器的话,它的电话号码将为5556,在输入手机号中输入5556,短信内容可任意写,点击发送,便可在5556那个模拟器接收到5554那个模拟器发来的信息。

 

7.另外,还有一个可行的方法,在工具上方菜单下有Window,下拉菜单下有Show View,选择other...里面有个Emulator Control,点击一下,将会出现这样的界面:


 

8.在这个视图里,可不用写程序和代码,便可发送短信到模拟器上,在那个Incoming number里输入5554模拟器,在选择SMS,在Message里输入短信内容,点击下面的send按钮,便可发送短信到模拟器上。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值