How to check user entered own phone number in EditText?

前端 未结 5 766
醉酒成梦
醉酒成梦 2021-02-08 10:26

Mobile number will be entered in an edittext by user on registration page in my Android application. How can I check that user entered his/her mobile number not other\'s ?

5条回答
  •  佛祖请我去吃肉
    2021-02-08 10:58

    public class MainActivity extends Activity{    
    
        Button submit;
        EditText contact;
        String phNo;
        ProgressDialog progress;
        Boolean wasMyOwnNumber = false;
    Boolean workDone = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            contact = (EditText)findViewById(R.id.mobileNumber);
            submit = (Button) findViewById(R.id.button1);
    
            submit.setOnClickListener(new OnClickListener()
            {
                    public void onClick(View v)
                    {
                        phNo = contact.getText().toString();
                        new CheckOwnMobileNumber().execute();
                        Toast.makeText(getApplicationContext(), phNo, Toast.LENGTH_LONG).show();
                    }
            });
    
    
    
        }
    
        private class CheckOwnMobileNumber extends AsyncTask
        {
            @Override
            protected void onPostExecute(String result)
            {
                // TODO Auto-generated method stub
                if(progress.isShowing())
                {
    
                    progress.dismiss();
                    // Check SMS Received or not after that open dialog date
                    /*if(SMSReceiver.str.equals(phNo))
                    {
                        Toast.makeText(getApplicationContext(), "Thanks for providing your          number.", Toast.LENGTH_LONG).show();
    wasMyOwnNumber=true;workDone=true;
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "Provide your own mobile number please.", Toast.LENGTH_LONG).show();
    wasMyOwnNumber=false;workDone=true;
                        return;
                    }*/
    
    
                }
            }
    
            @Override
            protected String doInBackground(String... params)
            {
                // TODO Auto-generated method stub
                String msg = phNo;
                try
                {
                    sendSMS(phNo, msg);
                    int count=0;
                         while(!workDone)
                              {count++;}
                }
                catch(Exception ex)
                {
                    Log.v("Exception :", ""+ex);
                }
                return null;
            }
    
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                progress = ProgressDialog.show(MainActivity.this, "","Checking Mobile Number...");
                progress.setIndeterminate(true);
                progress.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                super.onPreExecute();
            }
    
    }
    
        private void sendSMS(String phoneNumber, String message)
            {        
                //PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), 0);                
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber, null, message, null, null);        
            }
    
    public class SMSReceiver extends BroadcastReceiver
    {
    private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    Context mContext;
    private Intent mIntent;
    static String address, str = null;
    
    // Retrieve SMS
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        mIntent = intent;
    
        String action = intent.getAction();
    
        if(action.equals(ACTION_SMS_RECEIVED))
        {
            SmsMessage[] msgs = getMessagesFromIntent(mIntent);
            if (msgs != null)
            {
                for (int i = 0; i < msgs.length; i++)
                {
                    address = msgs[i].getOriginatingAddress();
                    str = msgs[i].getMessageBody().toString();
                }
            }   
    
            // ---send a broadcast intent to update the SMS received in the
            // activity---
            workDone=true;
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("SMS_RECEIVED_ACTION");
            broadcastIntent.putExtra("sms", str);
            context.sendBroadcast(broadcastIntent);
    
        }
    
    }
    
    public static SmsMessage[] getMessagesFromIntent(Intent intent)
    {
        Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
        byte[][] pduObjs = new byte[messages.length][];
    
        for (int i = 0; i < messages.length; i++)
        {
            pduObjs[i] = (byte[]) messages[i];
        }
    
        byte[][] pdus = new byte[pduObjs.length][];
        int pduCount = pdus.length;
        SmsMessage[] msgs = new SmsMessage[pduCount];
        for (int i = 0; i < pduCount; i++)
        {
            pdus[i] = pduObjs[i];
            msgs[i] = SmsMessage.createFromPdu(pdus[i]);
        }
        return msgs;
    }
    }
        }
    

提交回复
热议问题