如何在 Android 应用程序中从服务器接收 OPT
How to receive OPT from server in Android App
大家好我是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()
{
}
}
我想为此创建一个接收器代码 activity
如果我没记错,那么您想通过将 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() 方法中注册您的接收器并在您的 activity onDestoy() 方法中注销它
Google 有 strictly prohibited 使用 READ_SMS 权限从 SMS 获取 OTP。
由于我们不能再使用READ_SMS权限,Google已经给出了一些其他的选择来使用SMS Retriever API实现自动短信验证。使用 SMS Retriever API,我们可以在我们的 Android 应用程序中自动执行基于 SMS 的用户验证,无需用户手动输入验证码,也不需要任何额外的应用程序权限。
大家好我是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()
{
}
}
我想为此创建一个接收器代码 activity
如果我没记错,那么您想通过将 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() 方法中注册您的接收器并在您的 activity onDestoy() 方法中注销它
Google 有 strictly prohibited 使用 READ_SMS 权限从 SMS 获取 OTP。
由于我们不能再使用READ_SMS权限,Google已经给出了一些其他的选择来使用SMS Retriever API实现自动短信验证。使用 SMS Retriever API,我们可以在我们的 Android 应用程序中自动执行基于 SMS 的用户验证,无需用户手动输入验证码,也不需要任何额外的应用程序权限。