0

I think I'm in a bit of a catch 22 situation. I'm making an app that verifies the user's phone number by sending a text message to the number the user entered and then checking - that way it's definitely the user's phone number. This activity should only appear once, at the beginning - for the user to verify, and from then on activity 2 should be loaded.

I am trying to do this with :

//  when the form loads, check to see if phoneNo is in there
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String phoneNoCheck = sharedPreferences.getString("phonenumber","");

if (phoneNoCheck != null) {
    //  if it is in there, start the new Activity
    Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
    MainActivity.this.startActivity(myIntent);

}

But it doesn't seem to be working properly. For a start, I'm not even sure it should be null. When I turn off my phone and on again, it starts back at activity 1, asking for verification.

And I can't test it. I need to run the app on my device as I can't send an SMS from the emulator, and I'm unable to get to the files where sharedPreferences exist on my device, so I can't even see if it's being written correctly.

Any advice on how I might be able to see the MyData.xml on my phone and proceed with testing my app, or tell my how I may be able to improve my code ? I downloaded the Astro app but still couldn't find it.

Here's my code :

package com.example.chris.tutorialspoint;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.provider.Telephony;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.tutorialspoint.R;

public class MainActivity extends Activity {
    Button sendBtn;
    EditText txtphoneNo;

    String phoneNo;
    String origNumber;

    private BroadcastReceiver receiver;

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

        sendBtn = (Button) findViewById(R.id.btnSendSMS);
        txtphoneNo = (EditText) findViewById(R.id.editText);

        //  when the form loads, check to see if phoneNo is in there
        SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
        String phoneNoCheck = sharedPreferences.getString("phonenumber","");

        if (phoneNoCheck != null) {
            //  if it is in there, start the new Activity
            Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
            MainActivity.this.startActivity(myIntent);

        }

        //if it is not in there, proceed with phone number verification

        else {
            sendBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    sendSMSMessage();
                }
            });

            IntentFilter filter = new IntentFilter();
//        the thing we're looking out for is received SMSs
            filter.addAction("android.provider.Telephony.SMS_RECEIVED");

            receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent)

                {
                    Bundle extras = intent.getExtras();

                    if (extras == null)
                        return;

                    Object[] pdus = (Object[]) extras.get("pdus");
                    SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
                    origNumber = msg.getOriginatingAddress();

                    Toast.makeText(getApplicationContext(), "Originating number" + origNumber, Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), "Sent to number" + phoneNo, Toast.LENGTH_LONG).show();

                }

            };
            registerReceiver(receiver, filter);
        }
    }



    protected void sendSMSMessage() {
        phoneNo = txtphoneNo.getText().toString();

        //this is the SMS received
        String message = "Verification test code. Please ignore this message. Thank you.";

        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, message, null, null);
            Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();

            //if originating phone number is the same as the sent to number, save
            //and go to the next activity
            if (origNumber.equals(phoneNo)) {
                //save the phone number
                SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("phonenumber", phoneNo);
                editor.commit();

                Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
                MainActivity.this.startActivity(myIntent);
            }
        }

        catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS failed, please try again.", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }
        super.onDestroy();
    }
CHarris
  • 2,693
  • 8
  • 45
  • 71
  • `sharedPreferences.getString("phonenumber","");` Read the doc. You are explicit\ly asking for a non-null value, by providing a non-null default value. – njzk2 Mar 30 '17 at 20:14
  • "I can't send an SMS from the emulator" you should be sending the SMS _to_ the emulator, which you can definitely do: http://stackoverflow.com/questions/4325669/sending-and-receiving-text-using-android-emulator/4325836#4325836 – njzk2 Mar 30 '17 at 20:16
  • what you are doing looks like two-factor authentication. Most system that provide that send a code in the SMS, and ask the user to copy it in the app. That way, it works even if the user does not allow SMS access to your app (which, as a user, I would never do unless your app is related to handling my sms messages) – njzk2 Mar 30 '17 at 20:18
  • @njzk2 thanks, I'll look into sending to the emulator. With regards,'Read the doc' - obviously "" is not the same as null then, as I had thought. – CHarris Mar 30 '17 at 20:30

1 Answers1

1

What do you expect to happen here:

String phoneNoCheck = sharedPreferences.getString("phonenumber","");

if (phoneNoCheck != null) {

    ...
}

The method that you are calling, getString(String, String), uses the second parameter as the default value, that is, if no property is found, it will default to, as you have defined, an empty string, "".

You then try and compare this to null, which will never be equal.

Try changing it up to something like this:

String phoneNoCheck = sharedPreferences.getString("phonenumber", null);

// check if null or empty
if ( null == phoneNoCheck || phoneNoCheck.equals("") ) {

    // unregistered
}
Matt Clark
  • 27,671
  • 19
  • 68
  • 123