如何将StringRequest发送到BroadcastReceiver onReceiver上的服务器

时间:2017-04-14 10:29:18

标签: android broadcastreceiver android-volley

我正在开发一个接收消息详细信息(number,message_body)的应用程序,并通过BroadcastReceiver将这些数据发送到服务器。数据显示在Toast上,但数据无法到达服务器。方法getIncomingMessageData正在运行但sendRequest无效。

public void onReceive(Context context, Intent intent) {
    this.intent = intent;
    mContext = context;
    final Bundle bundle = intent.getExtras();
    try {
        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {
                SmsMessage currentMessage = msMessage.createFromPdu((byte[]) pdusObj[i]);
                phoneNumber = currentMessage.getDisplayOriginatingAddress();
                message = currentMessage.getDisplayMessageBody();
                message_time = String.valueOf(currentMessage.getTimestampMillis());
                Toast.makeText(mContext, "senderNum : " + phoneNumber + "\n message : " + message + "\n time : " + message_time,
                        Toast.LENGTH_LONG).show();
                sendRequest();
            }
        }
    } 
    catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);
    }
}

public void sendRequest() {
    Toast.makeText(mContext, "Method Called", Toast.LENGTH_LONG).show();
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Toast.makeText(mContext, response, Toast.LENGTH_SHORT).show();
            Log.i("Server Response", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(mContext, error.toString(), Toast.LENGTH_LONG).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("number", phoneNumber);
            params.put("msg_time", message_time);
            params.put("message", message);
            Log.d(TAG, "getParams: " + phoneNumber);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(mContext);
    requestQueue.add(stringRequest);

}

Android清单文件

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Activity.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Activity.HomeActivity"></activity>
    <receiver android:name=".other.IncomingSmsBroadcast">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </receiver>
</application>

0 个答案:

没有答案