Hey everyone i am new in android
and i am working on a app this app confirm the user info through Mobile Number and i am sending OTP
using server but i want when OTP
receive then OPT read automatically my app
this code send request to server and server send an OTP
Code is
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()
{
}
}
i want create a receiver code for this activity
if I am not wrong then you want to verify the mobile number of the user by sending the OTP to the desired mobile number and the app should read the OTP automatically and send the same OTP back to the server. if so, then this can be done by using Dynamic receiver.
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**
}
}
}};
Now you can send this otp back to the server by using SMS Manager(for offline) or by using HTTP request in your case.
Don't forget to register your receiver in doInBackground() method and unregister it in your activity onDestoy() method