如何从Android App中的服务器接收OPT

时间:2015-10-29 05:46:19

标签: android broadcastreceiver

嘿大家我是android新手  我正在开发一个应用程序,这个应用程序通过手机号码确认用户信息,我正在使用服务器发送OTP,但我希望在OTP收到时,OPT会自动读取我的应用

此代码向服务器和服务器发送请求发送OTP

代码

  public  class AsyncTaslMobile extends AsyncTask<String, Void, Void>
   {
    final String mobile_no =mobileNumber.getText().toString();
    @Override
    protected Void doInBackground(String... params) {


        String url = "http://my localhost.com/";
        StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response).getJSONObject("form");
                            String site = jsonResponse.getString("site"),
                                    network = jsonResponse.getString("network");
                            Log.d("Response-------"+site,"------>"+network);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Log.d("Response-------","------>"+response);
                        Toast.makeText(getApplicationContext(), ""+response, Toast.LENGTH_SHORT).show();
                    }
                },
                new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }
        )
        {
            protected Map<String, String> getParams()
            {
                Map<String, String>  params = new HashMap<>();
                // the POST parameters:
                params.put("mobile", mobile_no);
                params.put("akey","xxxxxxxxxxxxxx");
                Log.d("Value is ----------",">"+params);
                return params;
            }
        };
        Volley.newRequestQueue(getApplicationContext()).add(postRequest);
        return null;
    }
    protected void onPostExecute(String response) {
    }
    protected void onPreExecute() 
   {
    }
}

我想为此活动创建接收者代码

2 个答案:

答案 0 :(得分:1)

如果我没有错,那么你想通过将OTP发送到所需的手机号码来验证用户的手机号码,应用程序应该自动读取OTP并将相同的OTP发送回服务器。 如果是这样,那么这可以通过使用动态接收器来完成。

    private BroadcastReceiver Receiver = new BroadcastReceiver(){
            public static final String SMS_BUNDLE = "pdus";

            @Override
            public void onReceive(Context context, Intent intent) {

                // TODO Auto-generated method stub

                Bundle intentExtras = intent.getExtras();
                if (intentExtras != null) {
                    Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);

                    for (int i = 0; i < sms.length; ++i) {
                        SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                        String smsBody = smsMessage.getMessageBody().toString();//it will give the whole sms body which is sent from the server

// if smsBody contains words separated with space e.g. OTP 1234 then you can get the otp value by below code. 
                      //  String[] elements = smsBody.split(" ");
                      //  String otp=elements[1];//it will return otp value i.e. 1234
  //if smsBody contains only otp value e.g. 1234, then smsBody will become the OTP
           **use your HTTP request to send otp back to the server here**        

                    }

                }

            }};

现在,您可以使用SMS管理器(用于离线)或在您的情况下使用HTTP请求将此otp发送回服务器。

不要忘记在doInBackground()方法中注册您的接收器并在您的活动中取消注册onDestoy()方法

答案 1 :(得分:0)

Google 拥有 strictly prohibited 使用 READ_SMS 权限以从 SMS 获取 OTP。

由于我们无法再使用 READ_SMS 权限,因此 Google 提供了一些其他选择来使用 SMS Retriever API 实现自动短信验证。借助 SMS Retriever API,我们可以在我们的 Android 应用中自动执行基于 SMS 的用户验证,无需用户手动输入验证码,也无需任何额外的应用权限。

欲了解更多信息check out this tutorial