发短信

来源:互联网 发布:mysql客户端工具 编辑:程序博客网 时间:2024/04/18 12:51

只要是手机。都具备打电话、发短信这些基本功能,那么。今天我们首先了解下如何用Android发短信

 

1、新建工程

 

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

 

view plain
  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.       
  19.     <EditText    
  20.         android:layout_width="fill_parent"   
  21.         android:layout_height="200px"   
  22.         android:background="#ffffff"  
  23.         android:textColor="#000000"  
  24.         android:id="@+id/message"  
  25.         android:text="@string/message"  
  26.     />  
  27.       
  28.     <Button    
  29.         android:id="@+id/button"  
  30.         android:layout_width="fill_parent"   
  31.         android:layout_height="wrap_content"   
  32.         android:layout_centerInParent="true"   
  33.         android:text="@string/button"  
  34.     />  
  35.       
  36. </LinearLayout>  

 

3、配置res/values/Strings.xml

 

view plain
  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.   
  9. </resources>  

 

4、添加关键代码

 

view plain
  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.   
  13. public class SendMessage extends Activity {  
  14.     /** Called when the activity is first created. */  
  15.       
  16.       
  17.     private Button send;  
  18.       
  19.     private EditText address;  
  20.       
  21.     private EditText message;  
  22.       
  23.       
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.           
  29.         message = (EditText)findViewById(R.id.message);  
  30.         address = (EditText)findViewById(R.id.text);  
  31.         send = (Button)findViewById(R.id.button);  
  32.           
  33.         message.setOnClickListener(new EditText.OnClickListener(){  
  34.   
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 // TODO Auto-generated method stub  
  38.                 message.setText("");  
  39.             }  
  40.               
  41.         });  
  42.           
  43.         send.setOnClickListener(new Button.OnClickListener(){  
  44.   
  45.             @Override  
  46.             public void onClick(View v) {  
  47.                 // TODO Auto-generated method stub  
  48.                 String addre = address.getText().toString();  
  49.                 String mess = message.getText().toString();  
  50.                 SmsManager smsManager = SmsManager.getDefault();  
  51.                   
  52.                 if (isPhoneNumberValid(addre) == true ) { //&& iswithid70(mess) == true 可采用同样的方式校验短信内容、长度等  
  53.                     try {  
  54.                         PendingIntent mPI = PendingIntent.getBroadcast(SendMessage.this, 0, new Intent(), 0);  
  55.                         smsManager.sendTextMessage(addre, null, mess, mPI, null);  
  56.                     } catch (Exception e) {  
  57.                         // TODO: handle exception  
  58.                     }  
  59.                       
  60.                     Toast.makeText(SendMessage.this"发送成功",Toast.LENGTH_SHORT ).show();  
  61.                       
  62.                     address.setText("");  
  63.                     message.setText("");  
  64.                       
  65.                 } else {  
  66.                     Toast.makeText(SendMessage.this"***不符合",Toast.LENGTH_SHORT ).show();  
  67.                 }  
  68.             }  
  69.         });  
  70.     }  
  71.       
  72.       
  73.     public static boolean isPhoneNumberValid(String phoneNumber){  
  74.           
  75.         boolean isValid = false;  
  76.         String expression = "^//(?(//d{3})//)?[- ]?(//d{3})[- ]?(//d{5})$";  
  77.         String expression2 = "^//(?(//d{3})//)?[- ]?(//d{4})[- ]?(//d{4})$";  
  78.         CharSequence inputStr = phoneNumber;  
  79.           
  80.         Pattern pattern = Pattern.compile(expression);  
  81.           
  82.         Matcher matcher = pattern.matcher(inputStr);  
  83.           
  84.         Pattern pattern2 = Pattern.compile(expression2);  
  85.           
  86.         Matcher matcher2 = pattern2.matcher(inputStr);  
  87.           
  88.         if(matcher.matches()||matcher2.matches()){  
  89.             isValid = true;  
  90.         }  
  91.           
  92.           
  93.         return isValid;  
  94.           
  95.     }  
  96. }  

 

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

 

view plain
  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.   
  15.     </application>  
  16.   
  17.     <uses-permission android:name="android.permission.SEND_SMS">  
  18.       
  19.     </uses-permission>  
  20. </manifest>   

 

 

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

 

 

 

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

 

 

 

 

小结

 

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

view plain
  1. PendingIntent mPI = PendingIntent.getBroadcast(SendMessage.this, 0, new Intent(), 0);  
  2.                         smsManager.sendTextMessage(addre, null, mess, mPI, null);  

 

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

view plain
  1. <uses-permission android:name="android.permission.SEND_SMS">  

原创粉丝点击