早晨第一個學習程序,短信發送器,這個軟件僅僅實現了一些簡單的功能,後面會繼續增加,慢慢完善,這次的需求就是簡單的短信發送,界面的編寫。
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
期望大家多交流,多給小弟提需求,沒有需求的日子真難熬啊!!!!!!!!!!!