(1)先将设计的软件界面中的字符串存储起来
strings.xml是位于values文件夹下面
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Message</string>
<string name="action_settings">Settings</string>
<string name="number">请输入电话号码</string>
<string name="content">请输入短信内容</string>
<string name="button">发送</string>
<string name="success">发送完成</string>
</resources>
(2)在activity_main.xml里面设计软件界面
activity_main.xml是位于layout文件夹下面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.dqpi.message.MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/number"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/number"
android:id="@+id/number"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"
android:id="@+id/content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout>
(3)在MainActivity.java中写逻辑代码
package com.dqpi.message;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
/**
* 定义全局变量numberText和contentText
*/
private EditText numberText;
private EditText contentText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找number所在的地方
numberText = (EditText)this.findViewById(R.id.number);
//查找content所在的地方
contentText = (EditText)this.findViewById(R.id.content);
//查找button所在的地方
Button button = (Button)this.findViewById(R.id.button);
//对button进行响应事件处理
button.setOnClickListener(new ButtOnClickListener());
}
/**
* 处理button的响应事件
* @author Mr Li
*
*/
private final class ButtOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
//获得number的值
String number = numberText.getText().toString();
//获得content的值
String content = contentText.getText().toString();
//创建SmsManager对象
SmsManager manager = SmsManager.getDefault();
//短信内容
ArrayList<String> texts = manager.divideMessage(content);
for(String text:texts){
manager.sendTextMessage(number, null, text, null, null);
}
//短信发送完成的通知
Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
(4)看看R.java文件
R文件是在创建的android application project项目的gen文件夹下面,这个文件不需要自己手动修改。
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.dqpi.message;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
public static final int msm=0x7f020001;
}
public static final class id {
public static final int action_settings=0x7f080003;
public static final int button=0x7f080002;
public static final int content=0x7f080001;
public static final int number=0x7f080000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int main=0x7f070000;
}
public static final class string {
public static final int action_settings=0x7f050001;
public static final int app_name=0x7f050000;
public static final int button=0x7f050004;
public static final int content=0x7f050003;
public static final int number=0x7f050002;
public static final int success=0x7f050005;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
(5)部署到手机上面,进行功能测试。