只要是手机。都具备打电话、发短信这些基本功能,那么。今天我们首先了解下如何用Android发短信
1、新建工程
2、修改已经生成的main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/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/hello"
- />
- <EditText
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/text"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="200px"
- android:background="#ffffff"
- android:textColor="#000000"
- android:id="@+id/message"
- android:text="@string/message"
- />
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="@string/button"
- />
- </LinearLayout>
3、配置res/values/Strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">短信测试</string>
- <string name="app_name">短信测试</string>
- <string name="button">发送</string>
- <string name="text">13800138000</string>
- <string name="message">请输入短信内容</string>
- </resources>
4、添加关键代码
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import android.app.Activity;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.telephony.gsm.SmsManager;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class SendMessage extends Activity {
- /** Called when the activity is first created. */
- private Button send;
- private EditText address;
- private EditText message;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- message = (EditText)findViewById(R.id.message);
- address = (EditText)findViewById(R.id.text);
- send = (Button)findViewById(R.id.button);
- message.setOnClickListener(new EditText.OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- message.setText("");
- }
- });
- send.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- String addre = address.getText().toString();
- String mess = message.getText().toString();
- SmsManager smsManager = SmsManager.getDefault();
- if (isPhoneNumberValid(addre) == true ) { //&& iswithid70(mess) == true 可采用同样的方式校验短信内容、长度等
- try {
- PendingIntent mPI = PendingIntent.getBroadcast(SendMessage.this, 0, new Intent(), 0);
- smsManager.sendTextMessage(addre, null, mess, mPI, null);
- } catch (Exception e) {
- // TODO: handle exception
- }
- Toast.makeText(SendMessage.this, "发送成功",Toast.LENGTH_SHORT ).show();
- address.setText("");
- message.setText("");
- } else {
- Toast.makeText(SendMessage.this, "***不符合",Toast.LENGTH_SHORT ).show();
- }
- }
- });
- }
- public static boolean isPhoneNumberValid(String phoneNumber){
- boolean isValid = false;
- String expression = "^//(?(//d{3})//)?[- ]?(//d{3})[- ]?(//d{5})$";
- String expression2 = "^//(?(//d{3})//)?[- ]?(//d{4})[- ]?(//d{4})$";
- CharSequence inputStr = phoneNumber;
- Pattern pattern = Pattern.compile(expression);
- Matcher matcher = pattern.matcher(inputStr);
- Pattern pattern2 = Pattern.compile(expression2);
- Matcher matcher2 = pattern2.matcher(inputStr);
- if(matcher.matches()||matcher2.matches()){
- isValid = true;
- }
- return isValid;
- }
- }
5、修改AndroidManifest.xml,增加短信发送权限
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.sendMessage"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".SendMessage"
- 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>
- <uses-permission android:name="android.permission.SEND_SMS">
- </uses-permission>
- </manifest>
这样,整个demo就完成了 一下是运行效果
点击发送后 调用土司消息显示结果
小结
1.整体来说比较简单,主要用到了
- PendingIntent mPI = PendingIntent.getBroadcast(SendMessage.this, 0, new Intent(), 0);
- smsManager.sendTextMessage(addre, null, mess, mPI, null);
2.别忘记在AndroidManifest.xml里添加权限
- <uses-permission android:name="android.permission.SEND_SMS">