布局
[javascript]
<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:text="@string/input_number"
android:textColor="#FF0000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone" >
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_text"
android:textColor="#FF0000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="4" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/send"/>
</LinearLayout>
string.xml
[javascript]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">message</string>
<string name="action_settings">Settings</string>
<string name="input_number">請輸入手機號</string>
<string name="input_text">請輸入短信內容</string>
<string name="error">號碼或內容不能為空</string>
<string name="send">發送短信</string>
</resources>
配置權限
[javascript]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.message"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="12" />
<uses-permission android:name="android.permission.SEND_SMS"/>
//添加這一句
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.message.MainActivity"
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>
MainActivity.java
[java]
package com.example.message;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.Menu;
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 {
private EditText et_number;
private EditText et_content;
private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_content = (EditText) findViewById(R.id.et_content);
et_number = (EditText) findViewById(R.id.et_number);
send = (Button) findViewById(R.id.button1);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sendmessage();
}
});
}
public void sendmessage() {
String content = et_content.getText().toString();
String number = et_number.getText().toString();
if (TextUtils.isEmpty(number)) {
Toast.makeText(MainActivity.this, "號碼不能為空", 4).show();
return;
} else {
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(number, null, content, null, null);
}
}
@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;
}
}
需要注意的是
添加
sendTextMessage 添加包的時候,要添加
[java]
import android.telephony.SmsManager;
[java]
而不是這個包;
import android.telephony.gsm.SmsManager;