11

手机号码将由用户在我的 Android 应用程序的注册页面上以编辑文本形式输入。如何检查用户是否输入了他/她的手机号码而不是其他人的?

我试过这个:

TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
  mPhoneNumber = tMgr.getLine1Number();

并将此变量与 edittext 的文本进行比较。但在我的情况下,mPhoneNumber 返回NULL 。还有其他选择吗?如何解决这个问题?

任何帮助都是不言而喻的。

我试过这个:检查源代码:

     public class MainActivity extends Activity{    

    Button submit;
    EditText contact;
    String phNo;
    ProgressDialog progress;

    @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<String, Void, String>
    {
        @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();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Provide your own mobile number please.", Toast.LENGTH_LONG).show();
                    return;
                }*/

            }
        }

        @Override
        protected String doInBackground(String... params)
        {
            // TODO Auto-generated method stub
            String msg = phNo;
            try
            {
                sendSMS(phNo, msg);
            }
            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---
        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;
}
}

日志猫:

03-13 17:31:02.049: E/ActivityManager(161): ANR in com.example.test
03-13 17:31:02.049: E/ActivityManager(161): Reason: Broadcast of Intent { act=android.provider.Telephony.SMS_RECEIVED cmp=com.example.test/.SMSReceiver (has extras) }
03-13 17:31:02.049: E/ActivityManager(161):   54% 3732/com.example.test: 54% user + 0% kernel / faults: 21 minor
03-13 17:31:02.049: E/ActivityManager(161):   40% 3732/com.example.test: 40% user + 0% kernel / faults: 2 minor
03-13 17:31:30.699: I/ActivityManager(161): Killing com.example.test (pid=3732): user's request
03-13 17:31:30.799: I/ActivityManager(161): Process com.example.test (pid 3732) has died.
03-13 17:31:30.799: I/WindowManager(161): WIN DEATH: Window{40992f50 com.example.test/com.example.test.MainActivity paused=false}
03-13 17:31:30.819: E/InputDispatcher(161): channel '40818670 com.example.test/com.example.test.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
03-13 17:31:30.819: E/InputDispatcher(161): channel '40818670 com.example.test/com.example.test.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
03-13 17:34:59.649: I/ActivityManager(161): Start proc com.example.test for broadcast com.example.test/.SMSReceiver: pid=4037 uid=10098 gids={}
4

5 回答 5

10

它不能保证tMgr.getLine1Number();总是返回您的 SIM 卡号码。因为这取决于 SIM 卡中号码的可用性。就像我的情况一样,我的 Tre-Sweden SIM 卡不包含我的电话号码。

但是,如果您将 SIM 卡放入旧的索尼爱立信或诺基亚手机中,那么您将可以选择编辑此号码(在 SIM 上)。完成后,Android 设备将识别该号码并显示给您。

此外,如果您确实通过代码获取了您的电话号码,那么比较两个号码的最佳方法是:

boolean isSame = PhoneNumberUtils.compare(num1, num2);

或者,您可以实现某种密码验证逻辑(如 Viber、WhatsApp 或其他应用程序),在其中您要求用户在注册期间输入他们的电话号码。随后,该电话号码被发送到服务器,并针对该号码生成一个密码,该密码通过 SMS 发送给用户。最后,用户必须输入该密码(通过短信接收)才能完成注册。

或者

只需从用户的设备(经同意)向您的服务器/设备发送 SMS 并了解他们的电话号码。

于 2013-02-22T08:34:37.557 回答
5

获取使用的电话号码getLine1Number()既不安全也不确定

它被普遍接受,因为整个“获取电话号码”是多个问题的冲突,例如用户的隐私、运营商的品牌,甚至是供应商的。

无论如何,与 ios 不同的是,android 的android.provider.Telephony.SMS_RECEIVED整个过程对用户来说非常方便和无缝:您可以捕获短信并阅读它,而无需用户干预。

一种方法是什么?

在您的服务器上,收到验证电话号码的请求后,您应该生成一个密码tokenSent并将其发送到应用程序。现在,您的服务器应该通过短信将此代码发送到指定的电话号码。应用程序现在应该有一个注册的接收器来监听android.provider.Telephony.SMS_RECEIVED意图。收到后,应用程序会验证tokenSent是否与从服务器收到的相同。至此,电话注册完成,可以通知服务器了。

会出什么问题?

一般来说,此类应用程序通常是付费应用程序,用户不宜尝试任何事情。尽管如此,用户可能会输入他现在拥有的错误号码。然后在收到短信后,他可以将其转发到应用注册的手机。然后应用程序将收到tokenSent并错误地验证电话号码。

我们如何解决这个问题?

该解决方案的可行性取决于短信提供商是否允许您的服务器知道发件人的电话号码。这可能(AFAIK)不会发生,但如果发生了,那么你很幸运。这样,应用程序可以在收到tokenSent 后,将其与短信的发送者一起发送回服务器。然后服务器可以验证这是来自您的服务提供商的短信。

还有更可行的解决方案吗?(如果我真的很偏执)

在这种情况下,我相信最好的解决方案是从您的服务器请求tokenSent 。服务器将生成的tokenSent与输入的电话号码一起保存,并将此令牌发送到应用程序。该应用程序通知用户注册将花费他 1 条短信。一旦用户接受,您可以轻松地在后台发送一条包含此tokenSent的短信到某个服务。服务器一旦收到此tokenSent ,就会使用该令牌和短信的发送者来验证用户。当然,这对用户来说可能看起来有点骚扰和侵权,但它是最安全的方式,尤其是对于这种偏执狂(阅读这部分)。

手续:P

Add Permissions in Manifest

<uses-permission android:name="android.permission.RECEIVE_SMS">

Register the receiver(在将短信发送到手机之前执行此操作)

registerReceiver(new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getExtras() != null)
        {
            Object[] pdus = (Object[]) intent.getExtras().get("pdus");
            SmsMessage[] msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
               String from = msgs[i].getOriginatingAddress();                     
                String body = msgs[i].getMessageBody().toString();
                //here is the body
                //...
                unregisterReceiver(this); //If you are done with verification
            }
        }  
    }
}, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
于 2013-03-08T19:10:48.883 回答
4

我自己解决了。这是我的工作代码。 MainActivity 类:

   public class MainActivity extends Activity
{    
    Button submit;
    EditText contact;
    static String phNo;
    ProgressDialog progress;
    static Boolean wasMyOwnNumber;
    static Boolean workDone;
    final static int SMS_ROUNDTRIP_TIMOUT = 30000;

    @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);
        wasMyOwnNumber = false;
        workDone = false;
        submit.setOnClickListener(new OnClickListener()
        {
                public void onClick(View v)
                {
                    phNo = contact.getText().toString();
                    new CheckOwnMobileNumber().execute();
                }
        });
    }

    private class CheckOwnMobileNumber extends AsyncTask<String, Void, String>
    {
        @Override
        protected void onPostExecute(String result)
        {
            // TODO Auto-generated method stub
            if(progress.isShowing())
            {
                progress.dismiss();
                if(wasMyOwnNumber)
                {
                    Toast.makeText(getApplicationContext(), "Number matched.", Toast.LENGTH_LONG).show();
                    wasMyOwnNumber = false;
                    workDone = false;
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Wrong number.", Toast.LENGTH_LONG).show();
                    wasMyOwnNumber = false;
                    workDone = false;
                    return;
                }
            }
            super.onPostExecute(result);
        }

        @Override
        protected String doInBackground(String... params)
        {
            // TODO Auto-generated method stub
            String msg = phNo;
            try
            {
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phNo, null, msg, null, null);
                timeout();
            }
            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 boolean timeout()
    {
           int waited = 0;
           while (waited < SMS_ROUNDTRIP_TIMOUT)
           {
              try
              {
                Thread.sleep(100);
              }
              catch (InterruptedException e)
              {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              waited += 100;
              if(phoneNumberConfirmationReceived())
              {
                  waited=SMS_ROUNDTRIP_TIMOUT;
                  workDone = true;
              }
           }
           /*Log.v("MainActivity:timeout2: Waited: " , ""+waited);
           Log.v("MainActivity:timeout2:Comparision: ", ""+ phoneNumberConfirmationReceived());
           Log.v("MainActivity:timeout2: WorkDone value after wait complete : ", ""+workDone);*/
        return workDone;
    }

    private boolean phoneNumberConfirmationReceived()
    {
        if(wasMyOwnNumber)
        {
            workDone = true;
        }
        return workDone;
    }
}

短信接收器代码:

 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;
    boolean isSame;

    // 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();
                }
            }
            Log.v("Originating Address : Sender :", ""+address);
            Log.v("Message from sender :", ""+str);
            isSame = PhoneNumberUtils.compare(str, MainActivity.phNo);
            Log.v("Comparison :", "Yes this true. "+isSame);
            if(isSame)
            {
                 MainActivity.wasMyOwnNumber = isSame;
                 MainActivity.workDone=true;
            }

            // ---send a broadcast intent to update the SMS received in the
            // activity---
            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;
    }
}

未找到 ANR。

于 2013-03-15T09:34:47.620 回答
2
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<String, Void, String>
    {
        @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;
}
}
    }
于 2013-03-13T11:52:03.367 回答
1

只是想在上述答案的上述解释中添加一些内容。这也将为其他人节省时间。

就我而言,此方法没有返回任何手机号码,而是返回了一个空字符串。这是因为我将我的号码移植到了新的 sim 卡上。因此,如果我进入设置>关于电话>状态>我的电话号码,它会显示“未知”。

这可能是因为您已将号码从一个网络移植到另一个网络。

如果您无法从 API 检查号码然后:

一种方法是向该号码生成一条短信,然后向该手机号码发送一个随机生成的否。您必须要求用户将此随机生成的数字输入您的应用程序。将其输入应用程序后,您可以将其发送到服务器以检查文本中传递的号码是否正确(您已经针对该手机号码在服务器上保存了该号码)。

我希望这是有道理的。

于 2013-02-22T09:00:14.280 回答