早晨第一个学习程序,短信发送器,这个软件仅仅实现了一些简单的功能,后面会继续增加,慢慢完善,这次的需求就是简单的短信发送,界面的编写。
layout_main:
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/input_phone_num_text"
android:text="请输入手机号码"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/phone_num_edit"
android:layout_below="@id/input_phone_num_text"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/input_message_text"
android:layout_width="match_parent"
android:layout_below="@id/phone_num_edit"
android:text="请输入内容"
android:textSize="25sp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/input_message_edit"
android:layout_width="match_parent"
android:lines="8"
android:layout_below="@id/input_message_text"
android:layout_height="wrap_content" />
<Button
android:id="@+id/input_button"
android:layout_below="@id/input_message_edit"
android:layout_centerHorizontal="true"
android:text="发送"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Activity:
private Button button;
private EditText phoneNum;
private EditText messageContext;
private String phoneNumStr;
private String messageStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.input_button);
phoneNum = (EditText) findViewById(R.id.phone_num_edit);
messageContext = (EditText) findViewById(R.id.input_message_edit);
phoneNumStr = phoneNum.getText().toString().trim();
messageStr = messageContext.getText().toString().trim();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(phoneNumStr)|| TextUtils.isEmpty(messageStr)) {
Log.e("test", "this is a flag 1");
Toast.makeText(v.getContext(), "号码和内容不能为空", Toast.LENGTH_LONG).show();
} else {
Log.e("test", "this is a flag 2");
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> context = smsManager.divideMessage(messageStr);
for (String messageStr : context) {
smsManager.sendTextMessage(phoneNumStr, null, messageStr, null, null);
}
}
}
});
}
还有一个就是在mainfast文件中添加权限:
<uses-permission android:name="android.permission.SEND_SMS"/>
然后就可以运行了:
源码:
http://download.csdn.net/detail/xushuangshuang1/8595843
期望大家多交流,多给小弟提需求,没有需求的日子真难熬啊!!!!!!!!!!!