Android短信验证

时间:2013-09-11 06:19:45

标签: java android sms

您好我开发了一个Android应用程序..为我创建的登录页面...我已经创建了注册和登录选项的代码现在我需要一个短信验证过程,其中...在注册用户时收集移动电话号码并在注册时将短信从移动用户发送到提供的手机号码...并且短信必须是随机文本..并且用户需要在注册时键入此文本要成功......有谁知道怎么做这个请求帮助... 我正在提供我已经为登录页面开发的代码....

主要活动

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;

public class MainActivity extends Activity {


     Button btnSignIn,btnSignUp;
    LoginDataBaseAdapter loginDataBaseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        loginDataBaseAdapter=new LoginDataBaseAdapter(this);
        loginDataBaseAdapter=loginDataBaseAdapter.open();


        btnSignIn=(Button)findViewById(R.id.buttonSignIn);
        btnSignUp=(Button)findViewById(R.id.buttonSignUP);



        btnSignUp.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
                startActivity(intentSignUP);

            }
        });
    }

    public void signIn(View V)
    {
         final Dialog dialog = new Dialog(MainActivity.this);
         dialog.setContentView(R.layout.login);
         dialog.setTitle("Login");

         // get the References of views
         final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
         final  EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);

         Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);


         btnSignIn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String userName=editTextUserName.getText().toString();
                String password=editTextPassword.getText().toString();

                // fetch the Password form database for respective user name
                String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

                // check if the Stored password matches with  Password entered by user
                if(password.equals(storedPassword))
                {
                    Toast.makeText(MainActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                }
                else
                {
                    Toast.makeText(MainActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
                }

            }
        });


         dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Close The Database
        loginDataBaseAdapter.close();
}
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation = "vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/buttonSignIN"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Sign In"
        android:onClick="signIn" />

    <Button
        android:id="@+id/buttonSignUP"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttonSignIN"
        android:layout_alignParentLeft="true"
        android:text="Sign UP" />

</RelativeLayout>

signup.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"
    android:gravity="center_vertical" >


    <EditText
        android:id="@+id/editTextUserName"
        android:hint="User Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:hint="Password"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/editTextConfirmPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Confirm Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/buttonCreateAccount"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Create Account"
        android:layout_marginBottom="60dp" />


</LinearLayout>

login.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" >


        <EditText
            android:id="@+id/editTextUserNameToLogin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="User Name"
            android:ems="10" >
            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editTextPasswordToLogin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPassword"
            android:hint="Password" />

        <Button
            android:id="@+id/buttonSignIn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Sign In" />


    </LinearLayout>

1 个答案:

答案 0 :(得分:1)

您可以使用网络服务并在注册期间将移动设备发送到网络服务

然后使用算法根据手机号码生成唯一代码,并将此代码作为短信发送给用户

在Android应用中使用相同的算法生成取决于手机号码的唯一代码

每个手机号码只有一个唯一代码!!

所以这两个匹配,用户可以登录。

学习编写网络服务并在您的应用中使用

http://www.c-sharpcorner.com/uploadfile/88b6e5/how-to-call-web-service-in-android-using-soap/

http://www.codeproject.com/Articles/304302/Calling-Asp-Net-Webservice-ASMX-From-an-Android-Ap

http://programmertoolbox.wordpress.com/2013/04/07/communicate-between-asp-net-webservice-and-android-app/

http://www.codeproject.com/Articles/29305/Consuming-NET-Web-Services-via-the-kSOAP-library

代码例如。

                    import org.ksoap2.*;
                    import org.ksoap2.serialization.SoapObject;
                    import org.ksoap2.serialization.SoapPrimitive;
                    import org.ksoap2.serialization.SoapSerializationEnvelope;
                    import org.ksoap2.transport.*;

                    public class soapclass 
                    {
                    private String[] resultValue;
                    private String res;
                    final String NAMESPACE = "http://tempuri.org/"; //the namespace that you'll find in the header of your asmx webservice
                    String METHOD_NAME= "HelloWorld"; //the webservice method that you want to call
                    String SOAP_ACTION = NAMESPACE+METHOD_NAME;
                    final String URL = "http://192.168.1.3/Myapp/WebService1.asmx"; //the url of your webservice
                    public soapclass()
                    {

                    this.SOAP_ACTION =NAMESPACE+METHOD_NAME;
                    }
                    public String callHello()
                    {
                    try {

                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); //get the response from your webservice
                    res= response.toString();

                    }
                    catch (Exception e) {

                    res =e.getMessage();
                    }
                    return res;
                    }