You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SMSReceiver.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.rob57530.smsserver;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.URLEncoder;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.HttpStatus;
  8. import org.apache.http.StatusLine;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import android.content.BroadcastReceiver;
  14. import android.content.Context;
  15. import android.content.Intent;
  16. import android.content.SharedPreferences;
  17. import android.os.AsyncTask;
  18. import android.preference.PreferenceManager;
  19. import android.telephony.SmsMessage;
  20. import android.util.Base64;
  21. public class SMSReceiver extends BroadcastReceiver
  22. {
  23. class RequestTask extends AsyncTask<String, String, String>
  24. {
  25. @Override
  26. protected String doInBackground(String... uri)
  27. {
  28. HttpClient httpclient = new DefaultHttpClient();
  29. HttpResponse response;
  30. String responseString = null;
  31. try
  32. {
  33. HttpGet get = new HttpGet(uri[0]);
  34. get.setHeader("Host", uri[1]);
  35. get.setHeader("Authorization", "Basic " + Base64.encodeToString("__gates__:gatespasswd".getBytes(), Base64.NO_WRAP));
  36. response = httpclient.execute(get);
  37. StatusLine statusLine = response.getStatusLine();
  38. if (statusLine.getStatusCode() == HttpStatus.SC_OK)
  39. {
  40. ByteArrayOutputStream out = new ByteArrayOutputStream();
  41. response.getEntity().writeTo(out);
  42. out.close();
  43. responseString = out.toString();
  44. }
  45. else
  46. {
  47. response.getEntity().getContent().close();
  48. throw new IOException(statusLine.getReasonPhrase());
  49. }
  50. }
  51. catch (ClientProtocolException e)
  52. {
  53. e.printStackTrace();
  54. }
  55. catch (IOException e)
  56. {
  57. e.printStackTrace();
  58. }
  59. return responseString;
  60. }
  61. @Override
  62. protected void onPostExecute(String result)
  63. {
  64. super.onPostExecute(result);
  65. }
  66. }
  67. @Override
  68. public void onReceive(Context context, Intent intent)
  69. {
  70. SharedPreferences pr = PreferenceManager.getDefaultSharedPreferences(context);
  71. if (!pr.getBoolean("running", false))
  72. return;
  73. SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
  74. String http = p.getString("http", "");
  75. if(http.length() == 0)
  76. return;
  77. if(intent.getExtras() != null)
  78. {
  79. Object[] smsExtra = (Object[]) intent.getExtras().get("pdus");
  80. if(smsExtra != null)
  81. for(int i=0;i<smsExtra.length;i++)
  82. {
  83. try
  84. {
  85. SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
  86. RequestTask r = new RequestTask();
  87. r.execute("http://" + http + "/received.php?sender=" +
  88. URLEncoder.encode(sms.getOriginatingAddress(), "utf-8") + "&text=" +
  89. URLEncoder.encode(sms.getMessageBody().toString(), "utf-8"), http);
  90. }
  91. catch (UnsupportedEncodingException e)
  92. {
  93. e.printStackTrace();
  94. }
  95. }
  96. }
  97. }
  98. }