发送短信的关键程序是通过SmsManager对象的sendTextMessage()方法来完成,其中sendTextMessage()方法需传入五个值,依次是收件人地址(String),发送地址(String),发送服务(PendingIntent)与送达服务(PendingIntent),其中收件人与正文是不可为null的两个参数.
本例子通过两个模拟器,5554,5556互相通信,下面我将分5个步骤,讲一下发送短信程序是如何实现的.
Step 1:建立一个Android工程,我们命名为SMSDemo.
Step 2:设计一下程序的UI,也就是主界面main.xml,这里用AbsoluteLayout,有点丑见笑了!代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="收件 人:"
- android:textSize="16sp"
- android:layout_x="0px"
- android:layout_y="12px"
- >
- </TextView>
- <EditText
- android:id="@+id/myEditText1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=""
- android:textSize="18sp"
- android:layout_x="60px"
- android:layout_y="2px"
- >
- </EditText>
- <EditText
- android:id="@+id/myEditText2"
- android:layout_width="fill_parent"
- android:layout_height="223px"
- android:text=""
- android:textSize="18sp"
- android:layout_x="0px"
- android:layout_y="52px"
- >
- </EditText>
- <Button
- android:id="@+id/myButton1"
- android:layout_width="162px"
- android:layout_height="wrap_content"
- android:text="发送短信"
- android:layout_x="80px"
- android:layout_y="302px"
- >
- </Button>
- </AbsoluteLayout>
-
-
-
Step 3:主控制程序SMSDemo.java如下:
- package com.android.test;
-
- import android.app.Activity;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.telephony.gsm.SmsManager;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
-
- public class SMSDemo extends Activity {
-
- private Button mButton1;
- private EditText mEditText1;
- private EditText mEditText2;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
- mEditText1 = (EditText)findViewById(R.id.myEditText1);
- mEditText2 = (EditText)findViewById(R.id.myEditText2);
- mButton1 = (Button)findViewById(R.id.myButton1);
-
- mButton1.setOnClickListener(new Button.OnClickListener(){
-
- public void onClick(View v) {
-
- String messageAddress = mEditText1.getText().toString();
- String messageContent = mEditText2.getText().toString();
-
-
-
- SmsManager smsManager = SmsManager.getDefault();
-
- if(messageAddress.trim().length()!=0 && messageContent.trim().length()!=0)
- {
- try{
- PendingIntent pintent = PendingIntent.getBroadcast(SMSDemo.this, 0, new Intent(), 0);
- smsManager.sendTextMessage(messageAddress, null, messageContent, pintent, null);
-
- }catch(Exception e)
- {
- e.printStackTrace();
- }
-
- Toast.makeText(SMSDemo.this, "发送成功", Toast.LENGTH_LONG).show();
- }
- else{
- Toast.makeText(SMSDemo.this, "发送地址或者内容不能为空", Toast.LENGTH_SHORT).show();
- }
- }
-
- });
- }
- }
Step 4:增加拨打电话权限AndroidManifest.xml代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.test"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".SMSDemo"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- </application>
- <uses-sdk android:minSdkVersion="3" />
- <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
- </manifest>
-
Step 5:run it!效果图如下,5554给5556发送了一条短信:
本文上面部分出自 “Android_Tutor” 博客,请务必保留此出处http://weizhulin.blog.51cto.com/1556324/311699
本文上面部分出自 51CTO.COM技术博客
文章评论(0条评论)
登录后参与讨论