Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android应用程序中从服务器接收OPT_Android_Broadcastreceiver - Fatal编程技术网

如何在Android应用程序中从服务器接收OPT

如何在Android应用程序中从服务器接收OPT,android,broadcastreceiver,Android,Broadcastreceiver,大家好,我是android新手 我正在开发一个应用程序此应用程序通过手机号码确认用户信息,我正在使用服务器发送OTP,但我希望在OTP接收后选择自动读取我的应用程序 此代码向服务器发送请求,服务器发送一个OTP 代码为 public class AsyncTaslMobile extends AsyncTask<String, Void, Void> { final String mobile_no =mobileNumber.getText().toString

大家好,我是android新手 我正在开发一个应用程序此应用程序通过手机号码确认用户信息,我正在使用服务器发送
OTP
,但我希望在
OTP
接收后选择自动读取我的应用程序

此代码向服务器发送请求,服务器发送一个
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() 
   {
    }
}
public类AsyncTaslMobile扩展了AsyncTask任务
{
最后一个字符串mobile_no=mobileNumber.getText().toString();
@凌驾
受保护的Void doInBackground(字符串…参数){
字符串url=”http://my localhost.com/”;
StringRequest postRequest=新的StringRequest(Request.Method.POST,url,
新的Response.Listener(){
公共void onResponse(字符串响应){
试一试{
JSONObject jsonResponse=新的JSONObject(response).getJSONObject(“表单”);
String site=jsonResponse.getString(“site”),
network=jsonResponse.getString(“网络”);
Log.d(“响应----”+站点,-->“+网络”);
}捕获(JSONException e){
e、 printStackTrace();
}
Log.d(“响应----”,“-->”+响应);
Toast.makeText(getApplicationContext(),“”+响应,Toast.LENGTH\u SHORT.show();
}
},
新的Response.ErrorListener(){
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
}
}
)
{
受保护的映射getParams()
{
Map params=新的HashMap();
//POST参数:
参数put(“移动”,移动编号);
参数put(“akey”、“xxxxxxxxxxxx”);
Log.d(“值为-------”,“>”+参数);
返回参数;
}
};
newRequestQueue(getApplicationContext()).add(postRequest);
返回null;
}
受保护的void onPostExecute(字符串响应){
}
受保护的void onPreExecute()
{
}
}

我想为此活动创建一个接收器代码

如果我没有错,那么您想通过将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**        

                    }

                }

            }};
private BroadcastReceiver=new BroadcastReceiver(){
公共静态最终字符串SMS_BUNDLE=“PDU”;
@凌驾
公共void onReceive(上下文、意图){
//TODO自动生成的方法存根
Bundle intentExtras=intent.getExtras();
if(intentExtras!=null){
Object[]sms=(Object[])intentExtras.get(sms_BUNDLE);
对于(int i=0;i
现在,您可以使用SMS Manager(用于脱机)或使用HTTP请求将此otp发送回服务器

别忘了在doInBackground()方法中注册你的接收者,并在你的activity onDestoy()方法中注销它

谷歌有从短信获取OTP的读取短信权限

由于我们不能再使用READ_SMS权限,谷歌已经提供了一些其他的选择来实现自动短信验证。通过SMS Retriever API,我们可以在Android应用程序中自动执行基于SMS的用户验证,无需用户手动键入验证码,也无需任何额外的应用程序权限


有关更多信息。

您必须创建一个广播,该广播应能接收到来自您设备的传入短信。感谢回复,我有一条长消息。亲爱的会员,您的一次性验证cade是WL12。请验证您的手机号码,然后验证如何使用字符串[]elements=smsBody.split(“”)拆分msg@Baqiryou可以拆分msg;这将按空格分割消息,并将每个单词放入字符串的“elements”数组中。然后,您可以使用元素[position]获取otp,例如,在您的情况下,如msg是“亲爱的会员,您的一次性验证cade是WL12”,字符串otp=elements[8];从索引位置0开始,感谢WL12回复其包含逗号点,如“亲爱的会员,您的一次性密码为.WL12”。这样,它的字符串相同或不同。如果它具有固定格式,则您可以使用字符串函数或正则表达式搜索句子中的特定单词或特定字符后面的单词,如“在这种情况下。你必须提供一些逻辑,通过在谷歌上搜索很容易得到