android开发学习——注册时发送激活码

模仿一些网站在注册的时候要填写手机号码,然后点击获取激活码,将会产生一个激活码,用短信的方式发到注册者的手机,然后用户需要查看短信,将激活码填写在注册页面,有时间限制。若超时则需要重新发送。 

strings.xml,其代码如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="hello">Hello World, SendregActivity!</string>  
  5.     <string name="app_name">发送验证码</string>  
  6.     <string name="title">注册</string>  
  7.     <string name="links">我已阅读并接受:<a href="http://www.baidu.com">《百度用户协议》 </a>   </string>  
  8. </resources>  
  9.    

color.xml,其代码如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <resources>  
  3.    <color name="skyblue">#87CEEB</color><!--天蓝色 -->   
  4. </resources>  

main.xml,其代码如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6. <TableLayout    
  7.      android:layout_width="fill_parent"  
  8.     android:layout_height="wrap_content"  
  9.     >  
  10.     <TableRow   
  11.     android:gravity="center">  
  12.     <TextView   
  13.     android:layout_width="fill_parent"  
  14.     android:layout_height="wrap_content"  
  15.     android:textSize="20sp"  
  16.     android:text="手机号注册"/>  
  17.     </TableRow>  
  18.       
  19. </TableLayout>  
  20.  <TableLayout    
  21.      android:layout_width="fill_parent"  
  22.     android:layout_height="wrap_content"  
  23.     >  
  24.     <TableRow   
  25.     android:gravity="left">  
  26.     <TextView   
  27.     android:layout_width="fill_parent"  
  28.     android:layout_height="wrap_content"  
  29.     android:text="手机号:"/>  
  30.     <EditText   
  31.     android:layout_width="fill_parent"  
  32.     android:layout_height="wrap_content"  
  33.     android:hint="请输入手机号码..."  
  34.     android:id="@+id/phone"  
  35.         />  
  36.     </TableRow>  
  37.       
  38. </TableLayout>     
  39.     <TableLayout    
  40.      android:layout_width="fill_parent"  
  41.     android:layout_height="wrap_content"  
  42.     >  
  43.     <TableRow>  
  44.     <Button   
  45.     android:layout_width="fill_parent"  
  46.     android:layout_height="wrap_content"  
  47.     android:text="免费获取短信激活码"  
  48.     android:id="@+id/sendnumber"  
  49.         />  
  50.     </TableRow>  
  51.       
  52. </TableLayout>  
  53.     <TableLayout    
  54.      android:layout_width="fill_parent"  
  55.     android:layout_height="wrap_content"  
  56.     >  
  57.     <TableRow   
  58.     android:gravity="left">  
  59.     <TextView   
  60.     android:layout_width="fill_parent"  
  61.     android:layout_height="wrap_content"  
  62.     android:text="手短信激活码:"/>  
  63.     <EditText   
  64.     android:layout_width="fill_parent"  
  65.     android:layout_height="wrap_content"  
  66.     android:hint="输入激活码..."  
  67.     android:id="@+id/number"  
  68.         />  
  69.     <TextView   
  70.          android:layout_width="fill_parent"  
  71.          android:layout_height="wrap_content"  
  72.          android:id="@+id/counttime"  
  73.         />  
  74.     </TableRow>  
  75.       
  76. </TableLayout>     
  77. <TableLayout    
  78.      android:layout_width="fill_parent"  
  79.     android:layout_height="wrap_content"  
  80.     >  
  81.     <TableRow   
  82.     android:gravity="left">  
  83.     <TextView   
  84.     android:layout_width="fill_parent"  
  85.     android:layout_height="wrap_content"  
  86.     android:text="密码:"/>  
  87.     <EditText   
  88.     android:layout_width="fill_parent"  
  89.     android:layout_height="wrap_content"  
  90.     android:password="true"  
  91.     android:hint="请输入密码..."  
  92.     android:id="@+id/paw"  
  93.         />  
  94.     </TableRow>  
  95.       
  96. </TableLayout>    
  97. <TableLayout    
  98.      android:layout_width="fill_parent"  
  99.     android:layout_height="wrap_content"  
  100.     >  
  101.     <TableRow   
  102.     android:gravity="left">  
  103.     <CheckBox  
  104.     android:layout_width="fill_parent"  
  105.     android:layout_height="wrap_content"  
  106.     android:id="@+id/agree"   
  107.     android:checked="true"/>  
  108.     <TextView   
  109.     android:layout_width="fill_parent"  
  110.     android:layout_height="wrap_content"  
  111.     android:text="@string/links"/>  
  112.     </TableRow>  
  113.       
  114. </TableLayout>  
  115.    <TableLayout    
  116.      android:layout_width="fill_parent"  
  117.     android:layout_height="wrap_content"  
  118.     >  
  119.     <TableRow android:gravity="center">  
  120.     <Button   
  121.     android:layout_width="fill_parent"  
  122.     android:layout_height="wrap_content"  
  123.     android:textSize="30sp"  
  124.     android:text="注册"  
  125.     android:id="@+id/send"  
  126.     android:background="@color/skyblue"  
  127.         />  
  128.     </TableRow>  
  129.       
  130. </TableLayout>  
  131. </LinearLayout>  


下面来看主Activity文件,其内容如下:下面来看Activity文件,其代码如下:

[html]  view plain copy
  1. import java.util.logging.Logger;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. import android.app.Activity;  
  7. import android.app.PendingIntent;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10. import android.os.CountDownTimer;  
  11. import android.telephony.gsm.SmsManager;  
  12. import android.telephony.gsm.SmsMessage;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.CheckBox;  
  17. import android.widget.EditText;  
  18. import android.widget.TextView;  
  19. import android.widget.Toast;  
  20.   
  21. public class SendregActivity extends Activity {  
  22.     /** Called when the activity is first created. */  
  23.        
  24.     Button sendnumber,send;  
  25.     EditText phone,pwd,number;  
  26.     String temp="";  
  27.     String phonenum="";  
  28.     CheckBox agree;  
  29.     TimeCount timeco;  
  30.     TextView timecount;  
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.main);  
  35.         sendnumber=(Button) findViewById(R.id.sendnumber);  
  36.         send=(Button) findViewById(R.id.send);  
  37.         phone=(EditText) findViewById(R.id.phone);  
  38.         pwd=(EditText) findViewById(R.id.paw);  
  39.         number=(EditText) findViewById(R.id.number);//激活码  
  40.         agree=(CheckBox) findViewById(R.id.agree);  
  41.         timecount=(TextView) findViewById(R.id.counttime);  
  42.         timeco = new TimeCount(120000, 1000);//时间为120秒  
  43.         sendnumber.setOnClickListener(new OnClickListener() {  
  44.               
  45.             @Override  
  46.             public void onClick(View v) {  
  47.                 // TODO Auto-generated method stub  
  48.                 for(int i=0;i<5;i++){//产生一个五位数的激活码  
  49.                     int k=(int) (Math.random()*10);  
  50.                     temp+=k;  
  51.                 }  
  52.                   
  53.                 phonenum=phone.getText().toString().trim();  
  54.                 SmsManager smsmanger=SmsManager.getDefault();  
  55.                 if(isPhoneNumberValid(phonenum)){  
  56.                     PendingIntent mPI=PendingIntent.getBroadcast(SendregActivity.this, 0, new Intent(), 0);  
  57.                     smsmanger.sendTextMessage(phonenum, null, "你的激活码是:"+temp, mPI, null);  
  58.                     Toast.makeText(SendregActivity.this, "激活码发送成功!", Toast.LENGTH_LONG).show();  
  59.                      timeco.start();  
  60.                 }  
  61.                 else{  
  62.                     Toast.makeText(SendregActivity.this, "电话格式不正确,请检查!", Toast.LENGTH_LONG).show();  
  63.                 }  
  64.                   
  65.             }  
  66.         });  
  67.         send.setOnClickListener(new OnClickListener() {  
  68.               
  69.             @Override  
  70.             public void onClick(View v) {  
  71.                 // TODO Auto-generated method stub  
  72.                  phonenum=phone.getText().toString().trim();  
  73.                 if(!isPhoneNumberValid(phonenum)){  
  74.                     Toast.makeText(SendregActivity.this, "电话格式不正确,请检查!", Toast.LENGTH_LONG).show();  
  75.                 }  
  76.                 else if(phonenum.length()==0){  
  77.                     Toast.makeText(SendregActivity.this, "请输入电话号码!", Toast.LENGTH_LONG).show();  
  78.                 }  
  79.                 else if(number.getText().toString().trim().length()==0){  
  80.                     Toast.makeText(SendregActivity.this, "请输入激活码!", Toast.LENGTH_LONG).show();  
  81.                 }  
  82.                 else if(!number.getText().toString().equals(temp)){  
  83.                     Toast.makeText(SendregActivity.this, "激活码不正确!", Toast.LENGTH_LONG).show();  
  84.                 }  
  85.                 else if(pwd.getText().toString().trim().length()<6||pwd.getText().toString().trim().length()>12){  
  86.                     Toast.makeText(SendregActivity.this, "密码不能少于6为多余12位!!", Toast.LENGTH_LONG).show();  
  87.                 }  
  88.                 else if(!agree.isChecked()){  
  89.                     Toast.makeText(SendregActivity.this, "请先阅读百度协议!!", Toast.LENGTH_LONG).show();  
  90.                 }  
  91.                 else{  
  92.                     Toast.makeText(SendregActivity.this, "注册成功!", Toast.LENGTH_LONG).show();  
  93.                     timeco.cancel();  
  94.                 }  
  95.             }  
  96.         });  
  97.     }  
  98.     /*检查字符串是否为电话号码的方法,并回传true or false的判断值*/  
  99.     public static boolean isPhoneNumberValid(String mobiles){    
  100.          Matcher m = null;  
  101.         if(mobiles.trim().length()>0){  
  102.         Pattern p = Pattern.compile("^((13[0-9])|(15[0-3])|(15[7-9])|(18[0,5-9]))\\d{8}$");       
  103.         mp.matcher(mobiles);       
  104.         }  
  105.         else{  
  106.                
  107.             return false;  
  108.         }  
  109.         return m.matches();       
  110.     }   
  111.     /* 定义一个倒计时的内部类 */  
  112.     class TimeCount extends CountDownTimer {  
  113.     public TimeCount(long millisInFuture, long countDownInterval) {  
  114.     super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔  
  115.     }  
  116.     @Override  
  117.     public void onFinish() {//计时完毕时触发  
  118.    temp="";  
  119.    Toast.makeText(SendregActivity.this, "超时,需要重新发送激活码!", Toast.LENGTH_LONG).show();  
  120.     }  
  121.     @Override  
  122.     public void onTick(long millisUntilFinished){//计时过程显示  
  123.      
  124.     timecount.setText("倒计时:"+millisUntilFinished /1000+"秒");  
  125.     }  
  126.     }  
  127. }  


程序要调用SmsManager来发送短信,因此还需要授予改程序发送短信的权限,也就是在AndroidManifest.xml文件中增加如下代码:

[html]  view plain copy
  1. <uses-permission android:name="android.permission.SEND_SMS"/>  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值