java - Android 值到 Sqlite 表

标签 java android sqlite

您好,我正在开发一个Android应用程序,其中有一个登录和注册页面,当用户输入用户名和密码时,密码将发送到手机号码,之后他将进入注册页面,在那里他需要输入用户名,用于验证的密码和密码..所以我需要的是必须向表提供一个随机值,例如“1”或成功注册的其他值..否则像这样的“0”..任何人都可以帮忙..

注册 Activity

public class RegisterActivity extends Activity {


    LoginDataBaseAdapter loginDataBaseAdapter;
    Button btnReg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_xm);


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



        btnReg = (Button) findViewById (R.id.buttonRegister);

        final  EditText editTextUserName=(EditText)findViewById(R.id.editTextUserNameToLogin);
        final  EditText editTextPassword=(EditText)findViewById(R.id.editTextPasswordToLogin);
        final  EditText editTextMobileNumber = (EditText)findViewById(R.id.editText1);



        btnReg.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();
                String mobileNumber = editTextMobileNumber.getText().toString();

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



                String sd = getIntent().getStringExtra("number"); 
                String name = editTextUserName.getText().toString();



                // check if the Stored password matches with  Password entered by user
                if(password.equals(storedPassword) && (mobileNumber.equals(sd))) 
                {
                    Toast.makeText(RegisterActivity.this, "Congrats: Registration Successfull", Toast.LENGTH_LONG).show();


                    Intent in = new Intent(RegisterActivity.this,HomePageActivity.class);
                    startActivity(in);



                }
                else
                {
                    Toast.makeText(RegisterActivity.this, "User Name, Passcode or Password does not match", Toast.LENGTH_LONG).show();
                }


            }
        });


    }







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

      }
    }

注册 Activity

public class SignUpActivity extends Activity

{

    EditText editTextUserName,editTextPassword,editTextConfirmPassword, editMobileNumber;
    Button btnCreateAccount;



    // Progress Dialog
        private ProgressDialog pDialog;

        JSONParser jsonParser = new JSONParser();




    Random r = new Random();
    int number =r.nextInt(9999 - 1000) + 1000;



    LoginDataBaseAdapter loginDataBaseAdapter;


private static String url_create_data = "http://iascpl.com/app/create_data1.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup_xm);



        // get Instance  of Database Adapter
                loginDataBaseAdapter=new LoginDataBaseAdapter(this);
                loginDataBaseAdapter=loginDataBaseAdapter.open();

                // Get References of Views


                editTextUserName=(EditText)findViewById(R.id.editTextUserName);
                editTextPassword=(EditText)findViewById(R.id.editTextPassword);
                editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
                editMobileNumber = (EditText)findViewById(R.id.mobileNumber);




                btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);

                btnCreateAccount.setOnClickListener(new View.OnClickListener() {

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

                        String name=editTextUserName.getText().toString();
                        String password=editTextPassword.getText().toString();
                        String confirmPassword=editTextConfirmPassword.getText().toString();

                        String phoneNo = editMobileNumber.getText().toString();
                        String sms = Integer.toString(number);


                        //Intent intent = new Intent(SignUpActivity.this, RegisterActivity.class);

                        //intent.putExtra("number", sms + "");
                        //startActivity(intent);



                        //new CreateNewProduct().execute();


                        StringTokenizer st=new StringTokenizer(phoneNo,",");
                        while (st.hasMoreElements())

                        {

                            String tempMobileNumber = (String)st.nextElement();
                            if(tempMobileNumber.length()>0 && sms.trim().length()>0) 
                            {
                                sendSMS(tempMobileNumber, sms);

                            }


                            else 

                            {

                                Toast.makeText(getBaseContext(), 
                                        "Please enter both phone number and message.", 
                                        Toast.LENGTH_SHORT).show();
                            }





                        }








                        // check if any of the fields are vacant
                        if(name.equals("")||password.equals("")||confirmPassword.equals(""))
                        {
                                Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
                                return;
                        }
                        // check if both password matches
                        if(!password.equals(confirmPassword))
                        {
                            Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
                            return;
                        }
                        else
                        {
                            // Save the Data in Database
                           loginDataBaseAdapter.insertEntry(name, password);
                          Toast.makeText(getApplicationContext(), "Passcode is sent to the mobile number you provided. ", Toast.LENGTH_LONG).show();     


                            new CreateNewProduct().execute();

                           // Intent intent = new Intent(SignUpActivity.this, RegisterActivity.class);

                          //  intent.putExtra("number", sms + "");
                           // startActivity(intent);



                        }







                    }
                });

    }









    private void sendSMS(String phoneNumber, String message)
    {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

      //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        },new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       


    }













    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        loginDataBaseAdapter.close();
    }



    /**
     * Background Async Task to Create new product
     * */
    class CreateNewProduct extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(SignUpActivity.this);
            pDialog.setMessage("Creating a new account..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {





            String name = editTextUserName.getText().toString();
            String password = editTextPassword.getText().toString();
            String mobile = editMobileNumber.getText().toString();
            String sms = Integer.toString(number);

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            params.add(new BasicNameValuePair("password", password));
            params.add(new BasicNameValuePair("mobile", mobile));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_data,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product




                    Intent i = new Intent(SignUpActivity.this, RegisterActivity.class);

                    i.putExtra("number", sms + "");
                    startActivity(i);

                    //closing this screen
                    //finish();
                } else {
                    // failed to create product
                    return "false";



                }



            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        /*protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }*/


        protected void onPostExecute(String result)

        {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
                        if (result == "false")
            Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();
                        pDialog.dismiss();

        }


    }





}

最佳答案

您可以像这样生成不同的随机值。

System.currentTimeMillis(); //It return long value of current time. This value always will be unique.

关于java - Android 值到 Sqlite 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19900875/

相关文章:

java - 拍摄Java中Windows(.exe)上特定应用程序的屏幕截图

java - 使用 "verbose"选项包装 SLF4J

java - Android 谷歌地图不断崩溃

java - Spring Security 示例

来自 Application 类的 Android Marshmallow 权限

ios - ON DELETE CASCADE 在 ios 中的 sqlite3 中不起作用

java - 从ListView到RecyclerView

android - 智能电视IPTV应用程序教程和示例

performance - 在 SQLite 中,准备好的语句真的能提高性能吗?

iphone - SQLite MYSQL 字符编码