android短信实例

来源:互联网 发布:硬笔书法字帖 知乎 编辑:程序博客网 时间:2024/06/18 05:00

1、新建工程

2、修改已经生成的main.xml

[c-sharp] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <EditText
  13. android:id="@+id/text"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="@string/text"
  17. />
  18. <EditText
  19. android:layout_width="fill_parent"
  20. android:layout_height="200px"
  21. android:background="#ffffff"
  22. android:textColor="#000000"
  23. android:id="@+id/message"
  24. android:text="@string/message"
  25. />
  26. <Button
  27. android:id="@+id/button"
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_centerInParent="true"
  31. android:text="@string/button"
  32. />
  33. </LinearLayout>

3、配置res/values/Strings.xml

[c-sharp] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">短信测试</string>
  4. <string name="app_name">短信测试</string>
  5. <string name="button">发送</string>
  6. <string name="text">13800138000</string>
  7. <string name="message">请输入短信内容</string>
  8. </resources>

4、添加关键代码

[c-sharp] view plaincopyprint?
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. import android.app.Activity;
  4. import android.app.PendingIntent;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.telephony.gsm.SmsManager;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12. public class SendMessage extends Activity {
  13. /** Called when the activity is first created. */
  14. private Button send;
  15. private EditText address;
  16. private EditText message;
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. message = (EditText)findViewById(R.id.message);
  22. address = (EditText)findViewById(R.id.text);
  23. send = (Button)findViewById(R.id.button);
  24. message.setOnClickListener(new EditText.OnClickListener(){
  25. @Override
  26. public void onClick(View v) {
  27. // TODO Auto-generated method stub
  28. message.setText("");
  29. }
  30. });
  31. send.setOnClickListener(new Button.OnClickListener(){
  32. @Override
  33. public void onClick(View v) {
  34. // TODO Auto-generated method stub
  35. String addre = address.getText().toString();
  36. String mess = message.getText().toString();
  37. SmsManager smsManager = SmsManager.getDefault();
  38. if (isPhoneNumberValid(addre) == true ) { //&& iswithid70(mess) == true 可采用同样的方式校验短信内容、长度等
  39. try {
  40. PendingIntent mPI = PendingIntent.getBroadcast(SendMessage.this, 0,new Intent(), 0);
  41. smsManager.sendTextMessage(addre, null, mess, mPI,null);
  42. } catch (Exception e) {
  43. // TODO: handle exception
  44. }
  45. Toast.makeText(SendMessage.this, "发送成功",Toast.LENGTH_SHORT ).show();
  46. address.setText("");
  47. message.setText("");
  48. } else {
  49. Toast.makeText(SendMessage.this, "***不符合",Toast.LENGTH_SHORT ).show();
  50. }
  51. }
  52. });
  53. }
  54. public static boolean isPhoneNumberValid(String phoneNumber){
  55. boolean isValid = false;
  56. String expression = "^//(?(//d{3})//)?[- ]?(//d{3})[- ]?(//d{5})$";
  57. String expression2 = "^//(?(//d{3})//)?[- ]?(//d{4})[- ]?(//d{4})$";
  58. CharSequence inputStr = phoneNumber;
  59. Pattern pattern = Pattern.compile(expression);
  60. Matcher matcher = pattern.matcher(inputStr);
  61. Pattern pattern2 = Pattern.compile(expression2);
  62. Matcher matcher2 = pattern2.matcher(inputStr);
  63. if(matcher.matches()||matcher2.matches()){
  64. isValid = true;
  65. }
  66. return isValid;
  67. }
  68. }

5、修改AndroidManifest.xml,增加短信发送权限

[c-sharp] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.sendMessage"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".SendMessage"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. <uses-permission android:name="android.permission.SEND_SMS">
  16. </uses-permission>
  17. </manifest>

这样,整个demo就完成了 一下是运行效果

点击发送后 调用土司消息显示结果

小结

1.整体来说比较简单,主要用到了

[c-sharp] view plaincopyprint?
  1. PendingIntent mPI = PendingIntent.getBroadcast(SendMessage.this, 0,new Intent(), 0);
  2. smsManager.sendTextMessage(addre, null, mess, mPI,null);

2.别忘记在AndroidManifest.xml里添加权限

[c-sharp] view plaincopyprint?
  1. <uses-permission android:name="android.permission.SEND_SMS">

原创粉丝点击