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

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

strings.xml,其代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, SendregActivity!</string>
    <string name="app_name">发送验证码</string>
    <string name="title">注册</string>
    <string name="links">我已阅读并接受:<a href="http://www.baidu.com">《百度用户协议》 </a>   </string>
</resources>
 

color.xml,其代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
   <color name="skyblue">#87CEEB</color><!--天蓝色 --> 
</resources>

main.xml,其代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<TableLayout  
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TableRow 
    android:gravity="center">
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:text="手机号注册"/>
    </TableRow>
    
</TableLayout>
 <TableLayout  
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TableRow 
    android:gravity="left">
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="手机号:"/>
    <EditText 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="请输入手机号码..."
    android:id="@+id/phone"
        />
    </TableRow>
    
</TableLayout>   
    <TableLayout  
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TableRow>
    <Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="免费获取短信激活码"
    android:id="@+id/sendnumber"
        />
    </TableRow>
    
</TableLayout>
    <TableLayout  
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TableRow 
    android:gravity="left">
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="手短信激活码:"/>
    <EditText 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="输入激活码..."
    android:id="@+id/number"
        />
    <TextView 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/counttime"
        />
    </TableRow>
    
</TableLayout>   
<TableLayout  
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TableRow 
    android:gravity="left">
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="密码:"/>
    <EditText 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:password="true"
    android:hint="请输入密码..."
    android:id="@+id/paw"
        />
    </TableRow>
    
</TableLayout>  
<TableLayout  
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TableRow 
    android:gravity="left">
    <CheckBox
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/agree" 
    android:checked="true"/>
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/links"/>
    </TableRow>
    
</TableLayout>
   <TableLayout  
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TableRow android:gravity="center">
    <Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:text="注册"
    android:id="@+id/send"
    android:background="@color/skyblue"
        />
    </TableRow>
    
</TableLayout>
</LinearLayout>


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

import java.util.logging.Logger;

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.os.CountDownTimer;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SendregActivity extends Activity {
    /** Called when the activity is first created. */
	 
	Button sendnumber,send;
	EditText phone,pwd,number;
	String temp="";
	String phonenum="";
	CheckBox agree;
	TimeCount timeco;
	TextView timecount;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sendnumber=(Button) findViewById(R.id.sendnumber);
        send=(Button) findViewById(R.id.send);
        phone=(EditText) findViewById(R.id.phone);
        pwd=(EditText) findViewById(R.id.paw);
        number=(EditText) findViewById(R.id.number);//激活码
        agree=(CheckBox) findViewById(R.id.agree);
        timecount=(TextView) findViewById(R.id.counttime);
        timeco = new TimeCount(120000, 1000);//时间为120秒
        sendnumber.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				for(int i=0;i<5;i++){//产生一个五位数的激活码
					int k=(int) (Math.random()*10);
					temp+=k;
				}
				
			    phonenum=phone.getText().toString().trim();
				SmsManager smsmanger=SmsManager.getDefault();
				if(isPhoneNumberValid(phonenum)){
					PendingIntent mPI=PendingIntent.getBroadcast(SendregActivity.this, 0, new Intent(), 0);
					smsmanger.sendTextMessage(phonenum, null, "你的激活码是:"+temp, mPI, null);
					Toast.makeText(SendregActivity.this, "激活码发送成功!", Toast.LENGTH_LONG).show();
					 timeco.start();
				}
				else{
					Toast.makeText(SendregActivity.this, "电话格式不正确,请检查!", Toast.LENGTH_LONG).show();
				}
			    
			}
		});
        send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 phonenum=phone.getText().toString().trim();
				if(!isPhoneNumberValid(phonenum)){
					Toast.makeText(SendregActivity.this, "电话格式不正确,请检查!", Toast.LENGTH_LONG).show();
				}
				else if(phonenum.length()==0){
					Toast.makeText(SendregActivity.this, "请输入电话号码!", Toast.LENGTH_LONG).show();
				}
				else if(number.getText().toString().trim().length()==0){
					Toast.makeText(SendregActivity.this, "请输入激活码!", Toast.LENGTH_LONG).show();
				}
				else if(!number.getText().toString().equals(temp)){
					Toast.makeText(SendregActivity.this, "激活码不正确!", Toast.LENGTH_LONG).show();
				}
				else if(pwd.getText().toString().trim().length()<6||pwd.getText().toString().trim().length()>12){
					Toast.makeText(SendregActivity.this, "密码不能少于6为多余12位!!", Toast.LENGTH_LONG).show();
				}
				else if(!agree.isChecked()){
					Toast.makeText(SendregActivity.this, "请先阅读百度协议!!", Toast.LENGTH_LONG).show();
				}
				else{
					Toast.makeText(SendregActivity.this, "注册成功!", Toast.LENGTH_LONG).show();
					timeco.cancel();
				}
			}
		});
    }
    /*检查字符串是否为电话号码的方法,并回传true or false的判断值*/
    public static boolean isPhoneNumberValid(String mobiles){  
    	 Matcher m = null;
    	if(mobiles.trim().length()>0){
        Pattern p = Pattern.compile("^((13[0-9])|(15[0-3])|(15[7-9])|(18[0,5-9]))\\d{8}$");     
        m= p.matcher(mobiles);     
    	}
    	else{
    		 
    		return false;
    	}
        return m.matches();     
    } 
    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {
    public TimeCount(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
    }
    @Override
    public void onFinish() {//计时完毕时触发
   temp="";
   Toast.makeText(SendregActivity.this, "超时,需要重新发送激活码!", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onTick(long millisUntilFinished){//计时过程显示
   
    timecount.setText("倒计时:"+millisUntilFinished /1000+"秒");
    }
    }
}


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

 <uses-permission android:name="android.permission.SEND_SMS"/>


 


 


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值