1、<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SmsActivity!</string>
<string name="app_name">短信发送器</string>
<string name="input_number">请输入手机号</string>
<string name="input_text">请输入短信内容</string>
<string name="error">号码或内容不能为空</string>
<string name="send">发送短信</string>
</resources>
2、权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hbsi.csdn.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SmsActivity"
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>
</manifest>
3、页面设计:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#FF0000"
android:text="@string/input_number" />
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10" >
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#FF0000"
android:text="@string/input_text" />
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="4"
>
</EditText>
<Button
android:XonClick="sendmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send" />
</LinearLayout>
4、Activity:
package com.hbsi.csdn.sms;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class SmsActivity extends Activity {
private EditText et_number;
private EditText et_content;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_number = (EditText) this.findViewById(R.id.et_number);
et_content = (EditText) this.findViewById(R.id.et_content);
}
public void sendmessage(View view){
String content = et_content.getText().toString().trim();
String number = et_number.getText().toString().trim();
if(TextUtils.isEmpty(content)||TextUtils.isEmpty(number)){
Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();
return;
}else{
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(number, null, content, null,null );
}
}
}