关闭

Android 注册

标签: 注册
281人阅读 评论(0) 收藏 举报
分类:

标题

1:功能类:
public class Register extends Activity {
    private String tag_json_obj = "json_obj_req";
    private EditText phoneText;//手机号
    private EditText confirmPassword;//密码
    private Button Register;//提交
    private Context sContext;
    private CheckBox checkBox;//复选框
    private TextView user_terms;//服务条款
    private TextView privacy_clause;//隐私条款
    private Button code;//获取验证码
    private EditText codeText;//验证码框
    public static final int MSG_RECEIVED_CODE = 1;

    private SmsObserver mObserver;//内容观察者类
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == MSG_RECEIVED_CODE) {
                String code = (String)msg.obj;
                codeText.setText(code);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //动态获取短信验证码
        mObserver = new SmsObserver(Register.this, mHandler);
        Uri uri = Uri.parse("content://sms");
        getContentResolver().registerContentObserver(uri, true, mObserver);//注册短信内容观察者

        Register = (Button) findViewById(R.id.submit_register);//确认
        phoneText = (EditText) findViewById(R.id.phoneText);//手机号
        codeText = (EditText) findViewById(R.id.codeText);//输入验证码
        checkBox = (CheckBox) findViewById(R.id.agreement);//条款复选框
        user_terms = (TextView) findViewById(R.id.user_terms);//服务条款
        privacy_clause = (TextView) findViewById(R.id.privacy_clause);//隐私条款
        user_terms.setOnClickListener(listener1);
        privacy_clause.setOnClickListener(listener2);
        confirmPassword = (EditText) findViewById(R.id.confirmText);//密码
        Random r = new Random();
        final Integer i1 = r.nextInt(99999 - 0) + 99999;//随机产生六位数字
        //int random=(int)(Math.random()*1000000);
        code = (Button) findViewById(R.id.code);//验证码

        //验证码
        code.setOnClickListener(new View.OnClickListener() {
           @Override
            public void onClick(View v) {
                if (phoneText.getText().length() < 11) {
                    AlertDialog aDialog = new AlertDialog.Builder(Register.this).create();
                    aDialog.setTitle("手机号码错误");
                    aDialog.setMessage("请核对您的手机号码!");
                    aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "返回",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    aDialog.setIcon(R.drawable.login_48);
                    aDialog.show();
                } else {
                    RequestQueue queue = Volley.newRequestQueue(Register.this);

                    StringRequest myReq = new StringRequest(Request.Method.POST,
                            "http://www.jcpeixun.com/ashx/api/sendMRandom.aspx",
                            createMyReqSuccessListener(),

                            createMyReqErrorListener()) {
                                protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
                                Map<String, String> params = new HashMap<String, String>();
                                params.put("mobile", phoneText.getText().toString());
                                params.put("content", "您的验证码为" + i1.toString());
                                params.put("code", i1.toString());
                            return params;
                        }
                    };
                    AppController.getInstance().addToRequestQueue(myReq, tag_json_obj);

                    String url = MyURLEncoder.encodeURL("http://www.jcpeixun.com/ashx/api/sendMRandom.aspx?mobile=" + phoneText.getText() + "&content=您的验证码为" + i1.toString() + "&code=" + i1.toString());
                    Log.d("codeurl", url);
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    Log.d("code:", response.toString());
                                    // Display the first 500 characters of the response string.
                                    if (response.equalsIgnoreCase("1")) {
                                        AlertDialog aDialog = new AlertDialog.Builder(Register.this).create();
                                        aDialog.setTitle("发送验证码");
                                        aDialog.setMessage("已经向您的手机发送验证码!");
                                        aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "确定",
                                                new DialogInterface.OnClickListener() {
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        dialog.dismiss();

                                                    }
                                                });
                                        aDialog.setIcon(R.drawable.login_48);
                                        aDialog.show();
                                    } else if (response.equalsIgnoreCase("0")) {
                                        Toast.makeText(Register.this.getApplicationContext(), "错误", Toast.LENGTH_LONG).show();
                                    } else if (response.equalsIgnoreCase("-5")) {
                                        Toast.makeText(Register.this.getApplicationContext(), "异常", Toast.LENGTH_LONG).show();
                                    } else if (response.equalsIgnoreCase("-2")) {
                                        Toast.makeText(Register.this.getApplicationContext(), "参数有误", Toast.LENGTH_LONG).show();
                                    }
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(Register.this.getApplicationContext(), "请检测网络连接", Toast.LENGTH_LONG).show();
                        }
                    });
                    //queue.add(stringRequest);
                }
            }

        });

        Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!checkBox.isChecked()){
                    Toast.makeText(getApplication(),"亲,服务条款未同意哦!",Toast.LENGTH_SHORT).show();
                }else if (confirmPassword.equals(null)) {
                    Toast.makeText(getApplication(), "密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (i1.toString().equalsIgnoreCase(codeText.getText().toString())) {
                    final String url = getResources().getString(R.string.base_api) + "Register.aspx";
                    StringRequest myReq = new StringRequest(Request.Method.POST,
                            url,
                            createMyReqSuccessListener2(),

                            createMyReqErrorListener2()) {
                                protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
                                Map<String, String> params = new HashMap<String, String>();
                                params.put("utel", phoneText.getText().toString());
                                params.put("upass", confirmPassword.getText().toString());
                                return params;
                        }

                        ;
                    };

                    AppController.getInstance().addToRequestQueue(myReq, tag_json_obj);
                } else {
                    AlertDialog aDialog = new AlertDialog.Builder(Register.this).create();
                    aDialog.setTitle("错误");
                    aDialog.setMessage("验证码有误!");
                    aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "返回",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();

                                }
                            });
                    aDialog.show();
                }

            }

        });

        ImageButton goBack = (ImageButton) findViewById(R.id.goBack);
        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                redirectTo();
            }
        });

    }

    //调到登陆界面
    private void redirectTo() {
        Intent intent = new Intent(this, Login.class);
        startActivity(intent);
        finish();
    }
    //调到主页面
    private void redirectToMain() {
        Intent intent = new Intent(this, MyActivity.class);
        startActivity(intent);
        finish();
    }


    private Response.Listener<String> createMyReqSuccessListener() {
        return new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.equalsIgnoreCase("1")) {
                    AlertDialog aDialog = new AlertDialog.Builder(Register.this).create();
                    aDialog.setTitle("发送验证码");
                    aDialog.setMessage("已经向您的手机发送验证码!");
                    aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "确定",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();

                                }
                            });
                    aDialog.setIcon(R.drawable.login_48);
                    aDialog.show();
                } else if (response.equalsIgnoreCase("0")) {
                    Toast.makeText(Register.this.getApplicationContext(), "错误", Toast.LENGTH_LONG).show();
                } else if (response.equalsIgnoreCase("-5")) {
                    Toast.makeText(Register.this.getApplicationContext(), "异常", Toast.LENGTH_LONG).show();
                } else if (response.equalsIgnoreCase("-2")) {
                    Toast.makeText(Register.this.getApplicationContext(), "参数有误", Toast.LENGTH_LONG).show();
                } else if (response.equalsIgnoreCase("-1")) {
                    Toast.makeText(Register.this.getApplicationContext(), "该号码已注册", Toast.LENGTH_LONG).show();
                }
            }
        };
    }


    private Response.ErrorListener createMyReqErrorListener() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("fail:", error.toString());
                AlertDialog aDialog = new AlertDialog.Builder(Register.this).create();
                aDialog.setTitle(getResources().getString(R.string.error));
                aDialog.setMessage(getResources().getString(R.string.network_error));
                aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, getResources().getString(R.string.ok),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                aDialog.setIcon(R.drawable.login_48);
                aDialog.show();
            }
        };
    }


    private Response.Listener<String> createMyReqSuccessListener2() {
        return new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("success", response);

                Log.d("code:", response.toString());
                // Display the first 500 characters of the response string.
                AlertDialog aDialog = new AlertDialog.Builder(Register.this).create();
                aDialog.setTitle("注册成功");
                aDialog.setMessage("您已经注册成功!");
                aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, getResources().getString(R.string.ok),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                String url2 = "http://www.jcpeixun.com/app_client_api/login.aspx";
                                url2 = url2.concat("?username=");
                                url2 = url2.concat(phoneText.getText().toString());
                                url2 = url2.concat("&pwd=");
                                url2 = url2.concat(confirmPassword.getText().toString());
                                final String url = url2;
                                Log.d("url:", url);
                                // prepare the Request
                                String tag_json_obj = "json_obj_req";

                                final ProgressDialog pDialog = new ProgressDialog(Register.this);
                                pDialog.setMessage("正在登陆...");
                                pDialog.show();

                                JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                                        url, null,
                                        new Response.Listener<JSONObject>() {

                                            @Override
                                            public void onResponse(JSONObject response) {
                                                Log.d("response", response.toString());
                                                pDialog.dismiss();
                                                try {
                                                    String indicator = response.getString("error");
                                                    if (indicator.equalsIgnoreCase("0")) {
                                                        //登陆成功
                                                        //添加个人信息
                                                        SharedPreferences sp = getSharedPreferences("User", 0);
                                                        SharedPreferences.Editor Ed = sp.edit();
                                                        Ed.putString("username", response.getString("username"));
                                                        Ed.putString("uid", response.getString("uid"));
                                                        Ed.putString("grade", response.getString("grade"));
                                                        Ed.putString("authCourse", response.getString("authCourse"));
                                                        Ed.putString("QQ", response.getString("QQ"));
                                                        Ed.putString("password", confirmPassword.getText().toString());
                                                        Ed.putString("balance", response.getString("balance"));
                                                        Ed.putString("img", response.getString("img"));
                                                        Ed.commit();
                                                        redirectToMain();
                                                        //登陆成功填写资料
                                                        Intent intent = new Intent(Register.this, User_information.class);
                                                        startActivity(intent);
                                                        Toast.makeText(getApplicationContext(), "注册成功,请填写资料", Toast.LENGTH_SHORT).show();
                                                    } else {
                                                        AlertDialog aDialog = new AlertDialog.Builder(Register.this).create();
                                                        aDialog.setTitle("错误");
                                                        aDialog.setMessage(url);
                                                        aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "好",
                                                                new DialogInterface.OnClickListener() {
                                                                    public void onClick(DialogInterface dialog, int which) {
                                                                        dialog.dismiss();
                                                                    }
                                                                });
                                                        aDialog.show();
                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }

                                            }
                                        }, new Response.ErrorListener() {

                                    @Override
                                    public void onErrorResponse(VolleyError error) {
                                        VolleyLog.d("error", "Error: " + error.getMessage());
                                        // hide the progress dialog
                                        pDialog.dismiss();
                                    }
                                });

                                // Adding request to request queue
                                AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
                            }
                        });
                aDialog.show();
            }
        };
    }


    private Response.ErrorListener createMyReqErrorListener2() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("fail:", error.toString());
                AlertDialog aDialog = new AlertDialog.Builder(Register.this).create();
                aDialog.setTitle(getResources().getString(R.string.error));
                aDialog.setMessage(getResources().getString(R.string.network_error));
                aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, getResources().getString(R.string.ok),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                aDialog.show();
            }
        };
    }


    @Override
    protected void onPause() {
        super.onPause();
        getContentResolver().unregisterContentObserver(mObserver);//取消短信监听注册
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onEvent(sContext, "0x003");//友盟统计
        StatService.onEventDuration(Register.this, "0x0016", "注册", 100);//百度统计
    }

    //隐私条款监听器
   TextView.OnClickListener listener1 = new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           Intent intent = new Intent(Register.this,Clause.class);
           Bundle bundle = new Bundle();
           bundle.putString("data","1");
           intent.putExtras(bundle);
           startActivity(intent);
       }
   };

    TextView.OnClickListener listener2 = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Register.this,Clause.class);
            Bundle bundle = new Bundle();
            bundle.putString("data","2");
            intent.putExtras(bundle);
            startActivity(intent);
        }
    };
}

2:activity_register.xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f1f1f1"
    android:orientation="vertical"
    tools:context="com.plstudio.jichengandroid.ui.Register">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#169dde"
        android:orientation="horizontal">

        <!--25dp-->
        <ImageButton
            android:id="@+id/goBack"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:background="#169dde"
            android:scaleType="fitXY"
            android:src="@drawable/back" />

        <TextView
            android:id="@+id/textView15"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="注册"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ffffffff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="15dp"
        android:background="#ffffffff"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/phoneText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#00000000"
            android:ems="10"
            android:hint="请输入手机号码"
            android:inputType="phone" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:background="#ffffffff"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/codeText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#00000000"
            android:ems="10"
            android:hint="验证码"
            />

        <Button
            android:id="@+id/code"
            android:layout_width="80dp"
            android:layout_height="25dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="5dp"
            android:background="#169dde"
            android:text="获取验证码"
            android:textColor="#ffffffff"
            android:textSize="8dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffffff"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/confirmText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:background="#00000000"
            android:hint="密码:6-20位区分大小写"
            android:inputType="textPassword" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal">

        <CheckBox
            android:id="@+id/agreement"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:checked="true"
            android:textColor="#000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textSize="12dp"
            android:text="我已认真阅读并同意"
            android:textColor="#cccccc" />

        <TextView
            android:id="@+id/user_terms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textSize="12dp"
            android:text="用户条款"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/privacy_clause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textSize="12dp"
            android:text="隐私条款"
            android:textColor="#000000" />
    </LinearLayout>

    <Button
        android:id="@+id/submit_register"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/shape3"
        android:text="确认"
        android:textColor="#ffffffff"
        android:textSize="20dp" />

</LinearLayout>

3:效果图:

效果图

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:52950次
    • 积分:1122
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:12篇
    • 译文:4篇
    • 评论:3条
    最新评论