和上一篇文章一样,发短信在android开发中也是很常用的功能。
运行效果如图所示:
首先新建一个android project
在string.xml中定义需要用到的字符串:
- <?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
<string name="hello">Hello World, MainActivity!>
-
<string name="app_name">发短信程序</string>
-
<string name="str_input_phone_number">请输入手机号</string>
-
<string name="str_input_sms_content">请输入短信内容</string>
-
<string name="str_send_sms">发送短信</string>
-
<string name="str_remind_input_phone_number">请输入手机号</string>
-
<string name="str_remind_sms_send_finish">发送完成</string>
-
</resources>
在main.xml中编辑界面控件代码:
- <?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=""
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/str_input_phone_number"
-
/>
-
<EditText
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:id="@+id/phone_number_editText"
-
/>
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/str_input_sms_content"
-
/>
-
<EditText
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:id="@+id/sms_content_editText"
-
/>
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="@string/str_send_sms"
-
android:id="@+id/send_sms_button"
-
/>
-
</LinearLayout>
最后在MainActivity中编写发送短信的逻辑代码:
- package com.sms.ui;
-
-
import java.util.List;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.telephony.SmsManager;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.EditText;
-
import android.widget.Toast;
-
-
public class MainActivity extends Activity {
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
phone_number_editText = (EditText) findViewById(R.id.phone_number_editText);
-
sms_content_editText = (EditText) findViewById(R.id.sms_content_editText);
-
send_sms_button = (Button) findViewById(R.id.send_sms_button);
-
-
send_sms_button.setOnClickListener(new OnClickListener() {
-
-
@Override
-
public void onClick(View arg0) {
-
String phone_number = phone_number_editText.getText().toString().trim();
-
String sms_content = sms_content_editText.getText().toString().trim();
-
if(phone_number.equals("")) {
-
Toast.makeText(MainActivity.this, R.string.str_remind_input_phone_number, Toast.LENGTH_LONG).show();
-
} else {
-
SmsManager smsManager = SmsManager.getDefault();
-
if(sms_content.length() > 70) {
-
List<String> contents = smsManager.divideMessage(sms_content);
-
for(String sms : contents) {
-
smsManager.sendTextMessage(phone_number, null, sms, null, null);
-
}
-
} else {
-
smsManager.sendTextMessage(phone_number, null, sms_content, null, null);
-
}
-
Toast.makeText(MainActivity.this, R.string.str_remind_sms_send_finish, Toast.LENGTH_SHORT).show();
-
}
-
}
-
});
-
}
-
-
private EditText phone_number_editText;
-
private EditText sms_content_editText;
-
private Button send_sms_button;
-
}
在功能清单文件中声明发短信权限:
阅读(39882) | 评论(0) | 转发(0) |